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]
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
#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 287] Smart Network Administrator 二分答案+网络流
The citizens of a small village are tired of being the only inhabitants around without a connection ...
- spoj 287 NETADMIN - Smart Network Administrator【二分+最大流】
在spoj上用题号找题就已经是手动二分了吧 把1作为汇点,k个要入网的向t连流量为1的边,因为最小颜色数等于最大边流量,所以对于题目所给出的边(u,v),连接(u,v,c),二分一个流量c,根据最大流 ...
- SPOJ 0287 Smart Network Administrator
题目大意:一座村庄有N户人家.只有第一家可以连上互联网,其他人家要想上网必须拉一根缆线通过若干条街道连到第一家.每一根完整的缆线只能有一种颜色.网管有一个要求,各条街道内不同人家的缆线必须不同色,且总 ...
- SPOJ287 NETADMIN - Smart Network Administrator
传送门[洛谷] 常见套路? 关键点连新建汇点 流量1 源点1 原图中的边 二分流量. 二分+判满流 做完了. 附代码. #include<cstdio> #include<cstri ...
- Spoj-NETADMIN Smart Network Administrator
The citizens of a small village are tired of being the only inhabitants around without a connection ...
- SPOJ287 Smart Network Administrator(最大流)
题目大概是说,一个村庄有n间房子,房子间有m条双向路相连.1号房子有网络,有k间房子要通过与1号房子相连联网,且一条路上不能有同样颜色的线缆,问最少要用几种颜色的线缆. 二分枚举颜色个数,建立容量网络 ...
- 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 ...
- hihoCoder 1389 Sewage Treatment 【二分+网络流+优化】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
#1389 : Sewage Treatment 时间限制:2000ms 单点时限:2000ms 内存限制:256MB 描述 After years of suffering, people coul ...
- POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)
Secret Milking Machine Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9658 Accepted: ...
随机推荐
- BZOJ 1023: [SHOI2008]cactus仙人掌图 | 在仙人掌上跑DP
题目: 求仙人掌直径 http://www.lydsy.com/JudgeOnline/problem.php?id=1023 题解: 首先给出仙人掌的定义:满足所有的边至多在一个环上的无向联通图 我 ...
- 洛谷P2568 GCD (欧拉函数/莫比乌斯反演)
P2568 GCD 题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入输出格式 输入格式: 一个整数N 输出格式: 答案 输入输出样例 输入 ...
- POJ3660:Cow Contest(Floyd传递闭包)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16941 Accepted: 9447 题目链接 ...
- Codeforces Round #520 (Div. 2) A. A Prank
A. A Prank time limit per test 1 second memory limit per test 256 megabytes 题目链接:https://codefo ...
- javascript中arguments的应用——不定项传参求和
<script type="text/javascript"> window.onload=function(){ function sum(){ var result ...
- 【LA4670-Dominating Patterns】AC自动机
http://acm.hust.edu.cn/vjudge/problem/19224 题意:给定n个单词,一个字符串,问哪些单词在字符串中出现的次数最多.单词aba,文本ababa,则aba出现了2 ...
- Linux : 从私钥中提取公钥
已知一个私钥, 如何从其中提取公钥出来? 提取公钥 ssh-keygen -y -f /path/to/private_key > /path/to/public_key
- bzoj 2039 最小割模型
比较明显的网络流最小割模型,对于这种模型我们需要先求获利的和,然后减去代价即可. 我们对于第i个人来说, 如果选他,会耗费A[I]的代价,那么(source,i,a[i])代表选他之后的代价,如果不选 ...
- Django【进阶】权限管理
一.权限 RBAC:role basic access control 1.什么是权限: 一个权限就是一个url,不同个权限对应不同的url,拥有权限即可以访问这个url. 2.权限划分: 如 ...
- strcpy函数的实现【转】
转自:http://www.cnblogs.com/chenyg32/p/3739564.html 已知strcpy函数的原型是: char *strcpy(char *dst, const char ...