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 1Warning: large Input/Output data, be careful with certain languages
题意:给出n户人家,m条街道,只有第一户人家能够上网,其他家上网需要从第一家拉一根网线,每根网线只有一种颜色,
要求每条街道不同人家的网线颜色不同,求满足指定的k户人家都能够上网的最少颜色数量。
题解:那么就是求1到每个点的路径经过边次数的最大值的最小值。
我们可以二分答案,对于每次check,建边跑最大流即可。
有向图中是add(u,v,c)
无向图中 假设u到v的流量是x,v到u的流量是y,那就相当于u到v流了x+c-x=c
所以add(u,v,c) add(v,u,c)
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
const int Ni = 1010;
const int MAX = 1<<26;
struct Edge{
int u,v,c;
int next;
}edge[20*Ni];
int n,m;
int edn;//边数
int p[Ni];//父亲
int d[Ni];
int sp,tp;//原点,汇点
void addedge(int u,int v,int c)
{
edge[edn].u=u; edge[edn].v=v; edge[edn].c=c;
edge[edn].next=p[u]; p[u]=edn++;
edge[edn].u=v; edge[edn].v=u; edge[edn].c=0;
edge[edn].next=p[v]; p[v]=edn++;
}
int bfs()
{
queue <int> q;
memset(d,-1,sizeof(d));
d[sp]=0;
q.push(sp);
while(!q.empty())
{
int cur=q.front();
q.pop();
for(int i=p[cur];i!=-1;i=edge[i].next)
{
int u=edge[i].v;
if(d[u]==-1 && edge[i].c>0)
{
d[u]=d[cur]+1;
q.push(u);
}
}
}
return d[tp] != -1;
}
int dfs(int a,int b)
{
int r=0;
if(a==tp)return b;
for(int i=p[a];i!=-1 && r<b;i=edge[i].next)
{
int u=edge[i].v;
if(edge[i].c>0 && d[u]==d[a]+1)
{
int x=min(edge[i].c,b-r);
x=dfs(u,x);
r+=x;
edge[i].c-=x;
edge[i^1].c+=x;
}
}
if(!r)d[a]=-2;
return r;
}
int dinic(int sp,int tp)
{
int total=0,t;
while(bfs())
{
while(t=dfs(sp,MAX))
total+=t;
}
return total;
}
int ans[505],ps[250005],qs[250005];
int check(int t,int k){
edn=0;
memset(p,-1,sizeof(p));
int i,j;
for(i=1;i<=m;i++){
addedge(ps[i],qs[i],t);
addedge(qs[i],ps[i],t);
}
for(i=1;i<=k;i++){
addedge(ans[i],n+1,1);
}
if(dinic(sp,tp)==k)return 1;
return 0;
}
int main()
{
int i,u,v,c,t,k;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
for(i=1;i<=k;i++)scanf("%d",&ans[i]);
for(i=1;i<=m;i++)scanf("%d%d",&ps[i],&qs[i]);
sp=1;tp=n+1;
int l=1,r=k;
while(l+1<r){
int mid=l+r>>1;
if(check(mid,k))r=mid;
else l=mid;
}
if(check(l,k))printf("%d\n",l);
else printf("%d\n",r);
}
return 0;
}