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

【分析】颜色数相当于限定了经过每条边的cable数的上界,考虑二分答案res,现在要判定是否存在一系列从1出发分别到2,3,…,n的路径,使得经过每条边的路径数不超过res,一条路径就是一条流,建图跑最大流即可。
#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(二分)(网络流)的更多相关文章

  1. [SPOJ 287] Smart Network Administrator 二分答案+网络流

    The citizens of a small village are tired of being the only inhabitants around without a connection ...

  2. spoj 287 NETADMIN - Smart Network Administrator【二分+最大流】

    在spoj上用题号找题就已经是手动二分了吧 把1作为汇点,k个要入网的向t连流量为1的边,因为最小颜色数等于最大边流量,所以对于题目所给出的边(u,v),连接(u,v,c),二分一个流量c,根据最大流 ...

  3. SPOJ 0287 Smart Network Administrator

    题目大意:一座村庄有N户人家.只有第一家可以连上互联网,其他人家要想上网必须拉一根缆线通过若干条街道连到第一家.每一根完整的缆线只能有一种颜色.网管有一个要求,各条街道内不同人家的缆线必须不同色,且总 ...

  4. SPOJ287 NETADMIN - Smart Network Administrator

    传送门[洛谷] 常见套路? 关键点连新建汇点 流量1 源点1 原图中的边 二分流量. 二分+判满流 做完了. 附代码. #include<cstdio> #include<cstri ...

  5. Spoj-NETADMIN Smart Network Administrator

    The citizens of a small village are tired of being the only inhabitants around without a connection ...

  6. SPOJ287 Smart Network Administrator(最大流)

    题目大概是说,一个村庄有n间房子,房子间有m条双向路相连.1号房子有网络,有k间房子要通过与1号房子相连联网,且一条路上不能有同样颜色的线缆,问最少要用几种颜色的线缆. 二分枚举颜色个数,建立容量网络 ...

  7. 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 ...

  8. hihoCoder 1389 Sewage Treatment 【二分+网络流+优化】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)

    #1389 : Sewage Treatment 时间限制:2000ms 单点时限:2000ms 内存限制:256MB 描述 After years of suffering, people coul ...

  9. POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)

    Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9658   Accepted: ...

随机推荐

  1. Windows下查看某个端口被哪个服务占用

    1.查看某个端口是否被占用 打开命令行,输入:netstat -ano | findstr "3306" 2.查看端口被哪个服务占用 tasklist | findstr “PID ...

  2. HTML5之SVG详解(一):基本概括

    转载自:http://www.cnblogs.com/hupeng/archive/2012/12/21/2828456.html 1.背景 SVG是Scalable Vector Graphics的 ...

  3. vue遇到的坑(一)——数组更新

    最近在项目中遇到个问题,数组已经更新了,但是页面中的DOM并没有触发变化.我一直以来的想法就是: 既然vue实现的实时数据双向绑定,那么在model层发生了变化之后为什么就没有在view层更新呢? 在 ...

  4. [洛谷P2420] 让我们异或吧

    题目链接:让我们异或吧 题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中…xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor( B是 ...

  5. bzoj3918 [Baltic2014]Postman

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3918 [题解] 每日至少更一题啊qwq凑任务(迷 明显猜个结论:随便搜环就行了 然后搜环姿势 ...

  6. 【BZOJ】1592: [Usaco2008 Feb]Making the Grade 路面修整

    [算法]动态规划DP [题解] 题目要求不严格递增或不严格递减. 首先修改后的数字一定是原来出现过的数字,这样就可以离散化. f[i][j]表示前i个,第i个修改为第j个数字的最小代价,a表示排序后数 ...

  7. webstorm vue代码修改后不更新问题

    把 safe write 的勾去掉就行了.

  8. bzoj 2662&bzoj 2763 SPFA变形

    我们用dis[i,j]代表到i这个点,用j张票的最短路程,那么我们只需要在SPFA更新 的时候,用dis[i,j]更新dis[p,j]和dis[p,j+1]就行了 /***************** ...

  9. python 匿名函数和递归函数

    匿名函数lambda 匿名函数:lambda  x,y:x+y 上述解释:x,y分别是函数的参数,x+y是函数的返回值 匿名函数的命名规则,用lamdba 关键字标识,冒号(:)左侧表示函数接收的参数 ...

  10. (转载)常用的Python库

    http://forum.ubuntu.com.cn/viewtopic.php?f=63&t=249573&p=2640959 Tkinter ---- Python默认的图形界面接 ...