SPOJ NETADMIN - Smart Network Administrator 最大流+dinic 二分 模板

86 篇文章 0 订阅

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]
hh2 ... 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
...
emem2
[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

Illustration to the first example

Warning: large Input/Output data, be careful with certain languages


题意:

      给你一个村内的相互人家之间的联系,一号人家的房屋内有网络,现在有几户人家也想要网络,但是一根网线只能用一种颜色,走从1出现走一段连续的路,问最少需要多少种不同颜色的网线才能保证有需要的人家都有网络。

 

做法:

       题意其实就不好理解,但是理解了之后用二分的方法其实也挺好想到的,只要二分联系人家之间的流量,再将目标人家与超级汇点相连接,流量为1,如果该户人家可以到达那么肯定会流1进入超级汇点,最后判断汇点的流量是否为k即可。

    


代码如下:

#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<iostream>
using namespace std;
const int Ni = 550;
const int MAX = 1<<26;
struct Edge{
    int u,v,c;
    int next;
}edge[20*Ni];
int n,m,k;
int edn;//边数
int p[Ni];//父亲
int d[Ni];
int sp,tp;//原点,汇点
int in[Ni*Ni],out[Ni*Ni];
int a[Ni];
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 ck(int mid){
    edn=0;
    memset(p,-1,sizeof(p));
    for(int i=1;i<=m;i++){
        addedge(out[i],in[i],mid);
        addedge(in[i],out[i],mid);
    }
    for(int i=1;i<=k;i++){
        addedge(a[i],tp,1);
    }
    int ans=dinic(sp,tp);
    return ans>=k;
}
int main()
{
    int i,u,v,c,t,x;
    cin>>t;

    while(t--){
        scanf("%d%d%d",&n,&m,&k);
        sp=1;tp=501;
        for(int i=1;i<=k;i++){
            scanf("%d",&a[i]);
        }
        for(int i=1;i<=m;i++){
            scanf("%d%d",&out[i],&in[i]);
        }
        int l=0,r=m+1,ans;
        while(l<=r){
            int mid=(l+r)/2;
            if(ck(mid)){
                r=mid-1;
                ans=mid;
            }
            else l=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值