[SPOJ 287] 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
【题目大意】
一座村庄有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=,INF=;
int gi(){
int str=;char ch=getchar();
while(ch>'' || ch<'')ch=getchar();
while(ch>='' && ch<='')str=str*+ch-'',ch=getchar();
return str;
}
int head[N],num=,n,m,k,sta;
int s=,T=;
struct Lin{
int next,to,dis;
}a[N*N];
struct Edge{
int x,y;
}e[N*N];
int imp[N];
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=;
head[y]=num;
}
int q[N*],dep[N];
bool bfs()
{
memset(dep,,sizeof(dep));
int x,u,t=,sum=;
q[]=s;dep[s]=;
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<=)continue;
dep[u]=dep[x]+;q[++sum]=u;
}
}
return dep[T];
}
int dfs(int x,int flow){
if(x==T || !flow)return flow;
int u,sum=,tmp;
for(int i=head[x];i;i=a[i].next){
u=a[i].to;
if(dep[u]!=dep[x]+ || a[i].dis<=)continue;
tmp=dfs(u,min(flow,a[i].dis));
a[i].dis-=tmp;a[i^].dis+=tmp;
sum+=tmp;flow-=tmp;
if(!flow)break;
}
return sum;
}
int maxflow()
{
int qq,tot=;
while(bfs()){
qq=dfs(s,INF);
while(qq)tot+=qq,qq=dfs(s,INF);
}
return tot;
}
void Clear(){
memset(head,,sizeof(head));
num=;
}
bool check(int ff)
{
Clear();
for(int i=;i<=k;i++){
init(s,imp[i],);
}
for(int i=;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=;i<=k;i++)imp[i]=gi();
for(int i=;i<=m;i++){
e[i].x=gi();e[i].y=gi();
}
int l=,r=k,mid,ans;
while(l<=r){
mid=(l+r)>>;
if(check(mid))ans=mid,r=mid-;
else l=mid+;
}
printf("%d\n",ans);
}
int main()
{
//freopen("pp.in","r",stdin);
int T=gi();
while(T--){
work();
}
return ;
}
[SPOJ 287] Smart Network Administrator 二分答案+网络流的更多相关文章
- SPOJ NETADMIN - Smart Network Administrator(二分)(网络流)
NETADMIN - Smart Network Administrator #max-flow The citizens of a small village are tired of being ...
- SPOJ 0287 Smart Network Administrator
题目大意:一座村庄有N户人家.只有第一家可以连上互联网,其他人家要想上网必须拉一根缆线通过若干条街道连到第一家.每一根完整的缆线只能有一种颜色.网管有一个要求,各条街道内不同人家的缆线必须不同色,且总 ...
- Spoj-NETADMIN Smart Network Administrator
The citizens of a small village are tired of being the only inhabitants around without a connection ...
- bzoj1305: [CQOI2009]dance跳舞(二分答案+网络流)
1305: [CQOI2009]dance跳舞 题目:传送门 题解: 一眼网络流基础建模...然后就GG了 二分答案+拆点建边+最大流判断: 把男女生拆为男1,男2,女1,女2 1.男1和男2还有女1 ...
- spoj 287 NETADMIN - Smart Network Administrator【二分+最大流】
在spoj上用题号找题就已经是手动二分了吧 把1作为汇点,k个要入网的向t连流量为1的边,因为最小颜色数等于最大边流量,所以对于题目所给出的边(u,v),连接(u,v,c),二分一个流量c,根据最大流 ...
- SPOJ287 Smart Network Administrator(最大流)
题目大概是说,一个村庄有n间房子,房子间有m条双向路相连.1号房子有网络,有k间房子要通过与1号房子相连联网,且一条路上不能有同样颜色的线缆,问最少要用几种颜色的线缆. 二分枚举颜色个数,建立容量网络 ...
- SPOJ287 NETADMIN - Smart Network Administrator
传送门[洛谷] 常见套路? 关键点连新建汇点 流量1 源点1 原图中的边 二分流量. 二分+判满流 做完了. 附代码. #include<cstdio> #include<cstri ...
- SPOJ 7258 SUBLEX 后缀数组 + 二分答案 + 前缀和
Code: #include <cstdio> #include <algorithm> #include <cstring> #define setIO(s) f ...
- SPOJ 220 Relevant Phrases of Annihilation(后缀数组+二分答案)
[题目链接] http://www.spoj.pl/problems/PHRASES/ [题目大意] 求在每个字符串中出现至少两次的最长的子串 [题解] 注意到这么几个关键点:最长,至少两次,每个字符 ...
随机推荐
- 1013团队Beta冲刺day4
项目进展 李明皇 今天解决的进度 因服务器端未完成登录态维护,故无法进行前后端联动. 明天安排 前后端联动调试 林翔 今天解决的进度 因上课和实验室事务未完成登录态维护 明天安排 完成登录态维护 孙敏 ...
- 201421123042 《Java程序设计》第5周学习总结
1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 接口 Comparable Arrays.sort -has a Lambda表达式 1.2尝试使用思维导图将这些关键词组织起来 ...
- 第四十四条:为所有导出的API元素编写文档注释
简而言之,要为API编写文档,文档注释是最好,最有效的途径.对于所有可导出的API元素来说,使用文档注释应该被看作是强制性的.要 采用一致的风格来遵循标准的约定.记住,在文档注释内部出现任何的HTML ...
- C# 一个初学者对 依赖注入 IOC 的理解( 含 Unity 的使用)
通过 人打电话 来谈谈自己对IOC的理解 版本1.0 public class Person { public AndroidPhone Phone { get; set; } public void ...
- Linux知识积累(6) 系统目录及其用途
linux系统常见的重要目录以及各个目作用:/ 根目录.包含了几乎所有的文件目录.相当于中央系统.进入的最简单方法是:cd /./boot引导程序,内核等存放的目录.这个目录,包括了在引导过程中所必需 ...
- maven入门(1-3)maven的生命周期
maven的生命周期 maven的生命周期是抽象的,其实际行为都由插件来完成,引入maven 的 生命周期就是为了对所有的构建过程进行抽象和统一. 这种方式类似于模板方法,模板方法模式在父类中定义 ...
- lambda匿名函数透析
lambda匿名函数透析 目录 1 匿名函数的作用... 1 2 匿名函数的格式... 1 3 匿名函数实例代码... 3 1 匿名函数的作用 ...
- Python django实现简单的邮件系统发送邮件功能
Python django实现简单的邮件系统发送邮件功能 本文实例讲述了Python django实现简单的邮件系统发送邮件功能. django邮件系统 Django发送邮件官方中文文档 总结如下: ...
- angularjs中的run()方法使用
run方法用于初始化全局的数据,仅对全局作用域起作用. 举个栗子吧:<script type="text/javascript"> var m1 = angular.m ...
- 面向面试题和实际使用谈promise
"金三银四,金九银十",都是要收获的季节.面对各种面试题,各种概念.原理都要去记,挺枯燥的.本文是面向面试题和实际使用谈一下Promise. Promise是什么? Promise ...