您的位置:首页 > 理论基础 > 计算机网络

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

2017-05-14 20:03 381 查看
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

【题目大意】

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

求在指定的K户人家都能上网的前提下,最小的不同颜色种数。(1 <= N <= 500)

二分颜色种数X,每一条边的容量设为X 并且s->所有重要的用户,容量为1 跑一遍最大流 flow>=k 满足

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
using namespace std;
const int N=505,INF=1999999999;
int gi(){
int str=0;char ch=getchar();
while(ch>'9' || ch<'0')ch=getchar();
while(ch>='0' && ch<='9')str=str*10+ch-'0',ch=getchar();
return str;
}
int head
,num=1,n,m,k,sta;
int s=0,T=1;
struct Lin{
int next,to,dis;
}a[N*N];
struct Edge{
int x,y;
}e[N*N];
int imp
;
void init(int x,int y,int dis){
a[++num].next=head[x];
a[num].to=y;
a[num].dis=dis;
head[x]=num;
a[++num].next=head[y];
a[num].to=x;
a[num].dis=0;
head[y]=num;
}
int q[N*10],dep
;
bool bfs()
{
memset(dep,0,sizeof(dep));
int x,u,t=0,sum=1;
q[1]=s;dep[s]=1;
while(t!=sum)
{
x=q[++t];
for(int i=head[x];i;i=a[i].next){
u=a[i].to;
if(dep[u] || a[i].dis<=0)continue;
dep[u]=dep[x]+1;q[++sum]=u;
}
}
return dep[T];
}
int dfs(int x,int flow){
if(x==T || !flow)return flow;
int u,sum=0,tmp;
for(int i=head[x];i;i=a[i].next){
u=a[i].to;
if(dep[u]!=dep[x]+1 || a[i].dis<=0)continue;
tmp=dfs(u,min(flow,a[i].dis));
a[i].dis-=tmp;a[i^1].dis+=tmp;
sum+=tmp;flow-=tmp;
if(!flow)break;
}
return sum;
}
int maxflow()
{
int qq,tot=0;
while(bfs()){
qq=dfs(s,INF);
while(qq)tot+=qq,qq=dfs(s,INF);
}
return tot;
}
void Clear(){
memset(head,0,sizeof(head));
num=1;
}
bool check(int ff)
{
Clear();
for(int i=1;i<=k;i++){
init(s,imp[i],1);
}
for(int i=1;i<=m;i++){
init(e[i].x,e[i].y,ff);init(e[i].y,e[i].x,ff);
}
int tp=maxflow();
return tp>=k;
}
void work()
{
int x,y;
n=gi();m=gi();k=gi();
for(int i=1;i<=k;i++)imp[i]=gi();
for(int i=1;i<=m;i++){
e[i].x=gi();e[i].y=gi();
}
int l=0,r=k,mid,ans;
while(l<=r){
mid=(l+r)>>1;
if(check(mid))ans=mid,r=mid-1;
else l=mid+1;
}
printf("%d\n",ans);
}
int main()
{
//freopen("pp.in","r",stdin);
int T=gi();
while(T--){
work();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: