SPOJ NETADMIN - Smart Network Administrator(二分)(网络流)
NETADMIN - Smart Network Administrator
The citizens of a small village are tired of being the only inhabitants
around without a connection to the Internet. After nominating the future
network administrator, his house was connected to the global network.
All users that want to have access to the Internet must be connected
directly to the admin's house by a single cable (every cable may run
underground along streets only, from the admin's house to the user's
house). Since the newly appointed administrator wants to have everything
under control, he demands that cables of different colors should be
used. Moreover, to make troubleshooting easier, he requires that no two
cables of the same color go along one stretch of street.
Your goal is to find the minimum number of cable colors that must be
used in order to connect every willing person to the Internet.
Input
t [the number of test cases, t<=500]
n m k [n <=500 the number of houses (the index of the admin's house is 1)]
[m the number of streets, k the number of houses to connect]
h1 h2 ... hk [a list of k houses wanting to be conected to the network, 2<=hi<=n]
[The next m lines contain pairs of house numbers describing street ends]
e11 e12
e21 e22
...
em1 em2
[next cases]
Output
For each test case print the minimal number of cable colors necessary to make all the required connections.
Example
Input:
2
5 5 4
2 3 4 5
1 2
1 3
2 3
2 4
3 5
8 8 3
4 5 7
1 2
1 8
8 7
1 3
3 6
3 2
2 4
2 5 Output:
2
1
Warning: large Input/Output data, be careful with certain languages
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 2e3+;
const int M = ;
int n,m,k,f,d;
struct Edge{
int from,to,cap,flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct Dinic{
int s,t;
vector<Edge>edges;
vector<int> G[N];
bool vis[N];
int d[N];
int cur[N];
void init(){
for (int i=;i<=n+;i++)
G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap){
edges.push_back(Edge(from,to,cap,));
edges.push_back(Edge(to,from,,));
int mm=edges.size();
G[from].push_back(mm-);
G[to].push_back(mm-);
}
bool BFS(){
memset(vis,,sizeof(vis));
queue<int>q;
q.push(s);
d[s]=;
vis[s]=;
while (!q.empty()){
int x = q.front();q.pop();
for (int i = ;i<G[x].size();i++){
Edge &e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow){
vis[e.to]=;
d[e.to] = d[x]+;
q.push(e.to);
}
}
}
return vis[t];
} int DFS(int x,int a){
if (x==t || a==)
return a;
int flow = ,f;
for(int &i=cur[x];i<G[x].size();i++){
Edge &e = edges[G[x][i]];
if (d[x]+ == d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>){
e.flow+=f;
edges[G[x][i]^].flow-=f;
flow+=f;
a-=f;
if (a==)
break;
}
}
return flow;
} int Maxflow(int s,int t){
this->s=s;
this->t=t;
int flow = ;
while (BFS()){
memset(cur,,sizeof(cur));
flow+=DFS(s,inf);
}
return flow;
}
}dc;
int main(){
int T,u,v;
scanf("%d",&T);
while(T--){ vector<int>vec,edg;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<k;i++){
scanf("%d",&u);
vec.pb(u);
}
while(m--){
scanf("%d%d",&u,&v);
edg.push_back(u);edg.push_back(v);
}
int l=,r=n,ans=;
while(l<=r){
int mid=(l+r)/;
dc.init();
for(int i=;i<edg.size();i+=){
u=edg[i];v=edg[i+];
dc.AddEdge(u,v,mid);
dc.AddEdge(v,u,mid);
}
for(int i=;i<vec.size();i++){
u=vec[i];
dc.AddEdge(,u,);
}
if(dc.Maxflow(,)==k)r=mid-,ans=mid;
else l=mid+;
}
printf("%d\n",ans);
}
return ;
}
SPOJ NETADMIN - Smart Network Administrator(二分)(网络流)的更多相关文章
- [SPOJ 287] Smart Network Administrator 二分答案+网络流
The citizens of a small village are tired of being the only inhabitants around without a connection ...
- spoj 287 NETADMIN - Smart Network Administrator【二分+最大流】
在spoj上用题号找题就已经是手动二分了吧 把1作为汇点,k个要入网的向t连流量为1的边,因为最小颜色数等于最大边流量,所以对于题目所给出的边(u,v),连接(u,v,c),二分一个流量c,根据最大流 ...
- SPOJ 0287 Smart Network Administrator
题目大意:一座村庄有N户人家.只有第一家可以连上互联网,其他人家要想上网必须拉一根缆线通过若干条街道连到第一家.每一根完整的缆线只能有一种颜色.网管有一个要求,各条街道内不同人家的缆线必须不同色,且总 ...
- SPOJ287 NETADMIN - Smart Network Administrator
传送门[洛谷] 常见套路? 关键点连新建汇点 流量1 源点1 原图中的边 二分流量. 二分+判满流 做完了. 附代码. #include<cstdio> #include<cstri ...
- Spoj-NETADMIN Smart Network Administrator
The citizens of a small village are tired of being the only inhabitants around without a connection ...
- SPOJ287 Smart Network Administrator(最大流)
题目大概是说,一个村庄有n间房子,房子间有m条双向路相连.1号房子有网络,有k间房子要通过与1号房子相连联网,且一条路上不能有同样颜色的线缆,问最少要用几种颜色的线缆. 二分枚举颜色个数,建立容量网络 ...
- routing decisions based on paths, network policies, or rule-sets configured by a network administrator
https://en.wikipedia.org/wiki/Border_Gateway_Protocol Border Gateway Protocol (BGP) is a standardize ...
- hihoCoder 1389 Sewage Treatment 【二分+网络流+优化】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
#1389 : Sewage Treatment 时间限制:2000ms 单点时限:2000ms 内存限制:256MB 描述 After years of suffering, people coul ...
- POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)
Secret Milking Machine Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9658 Accepted: ...
随机推荐
- C#范型实例化对象
T s = System.Activator.CreateInstance<T>();
- BZOJ3242 [Noi2013]快餐店 【环套树 + 单调队列dp】
题目链接 BZOJ3242 题解 题意很清楚,找一点使得最远点最近 如果是一棵树,就是直径中点 现在套上了一个环,我们把环单独拿出来 先求出环上每个点外向树直径更新答案,并同时求出环上每个点外向的最远 ...
- angularjs的验证信息的写法
<div ng-messages="alarmDelayForm.alarmRuleName.$error" role="alert"> <d ...
- ActiveMQ(2) ActiveMQ创建HelloWorld
启动ActiveMQ: 请参见:ActiveMQ(1) 初识ActiveMQ 创建Maven工程: pom文件: <project xmlns="http://maven.apache ...
- vue+koa+mysql简易demo
功能支持网址收藏编辑 代码: https://github.com/lanleilin/lanOdyssey/tree/master/vueKoa/webCollection1 运行方法: 在serv ...
- redis连接池自动释放
http://blog.itpub.net/29485627/viewspace-1977880/
- COGS2090 Asm.Def找燃料
时间限制:1 s 内存限制:256 MB [题目描述] “听说咱们要完了?”比利·海灵顿拨弄着操纵杆,头也不回地问Asm.Def. “不要听得风就是雨.” “开个玩笑嘛.不就是打机器人,紧张啥,你 ...
- ubuntu 安装wxpython以及boa-constructor
直接参考 官方的安装文档. 学习python 的时候就 用 wxPython . 那个时候用的是windows 的版本. 现在 用 ubuntu 下开发了.没有搭建好环境. 其实就一句话: sudo ...
- Codeforces 219D Choosing Capital for Treeland 2次DP
//选择一个根使得变换最少边的方向使得能够到达所有点#include <map> #include <set> #include <list> #include & ...
- 使用MXNet远程编写卷积神经网络用于多标签分类
最近试试深度学习能做点什么事情.MXNet是一个与Tensorflow类似的开源深度学习框架,在GPU显存利用率上效率高,比起Tensorflow显著节约显存,并且天生支持分布式深度学习,单机多卡.多 ...