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]
h1h2 ... 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]
e11e12
e21e22
...
em1em2
[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

SPOJ NETADMIN - Smart Network Administrator(二分)(网络流)

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


下一篇:SPOJ 220 Relevant Phrases of Annihilation(后缀数组+二分答案)