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

  1. Input:
  2. 2
  3. 5 5 4
  4. 2 3 4 5
  5. 1 2
  6. 1 3
  7. 2 3
  8. 2 4
  9. 3 5
  10. 8 8 3
  11. 4 5 7
  12. 1 2
  13. 1 8
  14. 8 7
  15. 1 3
  16. 3 6
  17. 3 2
  18. 2 4
  19. 2 5
  20.  
  21. Output:
  22. 2
  23. 1

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

【分析】颜色数相当于限定了经过每条边的cable数的上界,考虑二分答案res,现在要判定是否存在一系列从1出发分别到2,3,…,n的路径,使得经过每条边的路径数不超过res,一条路径就是一条流,建图跑最大流即可。
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <string>
  7. #include <map>
  8. #include <stack>
  9. #include <queue>
  10. #include <vector>
  11. #define inf 0x3f3f3f3f
  12. #define met(a,b) memset(a,b,sizeof a)
  13. #define pb push_back
  14. typedef long long ll;
  15. using namespace std;
  16. const int N = 2e3+;
  17. const int M = ;
  18. int n,m,k,f,d;
  19. struct Edge{
  20. int from,to,cap,flow;
  21. Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
  22. };
  23. struct Dinic{
  24. int s,t;
  25. vector<Edge>edges;
  26. vector<int> G[N];
  27. bool vis[N];
  28. int d[N];
  29. int cur[N];
  30. void init(){
  31. for (int i=;i<=n+;i++)
  32. G[i].clear();
  33. edges.clear();
  34. }
  35. void AddEdge(int from,int to,int cap){
  36. edges.push_back(Edge(from,to,cap,));
  37. edges.push_back(Edge(to,from,,));
  38. int mm=edges.size();
  39. G[from].push_back(mm-);
  40. G[to].push_back(mm-);
  41. }
  42. bool BFS(){
  43. memset(vis,,sizeof(vis));
  44. queue<int>q;
  45. q.push(s);
  46. d[s]=;
  47. vis[s]=;
  48. while (!q.empty()){
  49. int x = q.front();q.pop();
  50. for (int i = ;i<G[x].size();i++){
  51. Edge &e = edges[G[x][i]];
  52. if (!vis[e.to] && e.cap > e.flow){
  53. vis[e.to]=;
  54. d[e.to] = d[x]+;
  55. q.push(e.to);
  56. }
  57. }
  58. }
  59. return vis[t];
  60. }
  61.  
  62. int DFS(int x,int a){
  63. if (x==t || a==)
  64. return a;
  65. int flow = ,f;
  66. for(int &i=cur[x];i<G[x].size();i++){
  67. Edge &e = edges[G[x][i]];
  68. if (d[x]+ == d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>){
  69. e.flow+=f;
  70. edges[G[x][i]^].flow-=f;
  71. flow+=f;
  72. a-=f;
  73. if (a==)
  74. break;
  75. }
  76. }
  77. return flow;
  78. }
  79.  
  80. int Maxflow(int s,int t){
  81. this->s=s;
  82. this->t=t;
  83. int flow = ;
  84. while (BFS()){
  85. memset(cur,,sizeof(cur));
  86. flow+=DFS(s,inf);
  87. }
  88. return flow;
  89. }
  90. }dc;
  91. int main(){
  92. int T,u,v;
  93. scanf("%d",&T);
  94. while(T--){
  95.  
  96. vector<int>vec,edg;
  97. scanf("%d%d%d",&n,&m,&k);
  98. for(int i=;i<k;i++){
  99. scanf("%d",&u);
  100. vec.pb(u);
  101. }
  102. while(m--){
  103. scanf("%d%d",&u,&v);
  104. edg.push_back(u);edg.push_back(v);
  105. }
  106. int l=,r=n,ans=;
  107. while(l<=r){
  108. int mid=(l+r)/;
  109. dc.init();
  110. for(int i=;i<edg.size();i+=){
  111. u=edg[i];v=edg[i+];
  112. dc.AddEdge(u,v,mid);
  113. dc.AddEdge(v,u,mid);
  114. }
  115. for(int i=;i<vec.size();i++){
  116. u=vec[i];
  117. dc.AddEdge(,u,);
  118. }
  119. if(dc.Maxflow(,)==k)r=mid-,ans=mid;
  120. else l=mid+;
  121. }
  122. printf("%d\n",ans);
  123. }
  124. return ;
  125. }

SPOJ NETADMIN - Smart Network Administrator(二分)(网络流)的更多相关文章

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

    The citizens of a small village are tired of being the only inhabitants around without a connection ...

  2. spoj 287 NETADMIN - Smart Network Administrator【二分+最大流】

    在spoj上用题号找题就已经是手动二分了吧 把1作为汇点,k个要入网的向t连流量为1的边,因为最小颜色数等于最大边流量,所以对于题目所给出的边(u,v),连接(u,v,c),二分一个流量c,根据最大流 ...

  3. SPOJ 0287 Smart Network Administrator

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

  4. SPOJ287 NETADMIN - Smart Network Administrator

    传送门[洛谷] 常见套路? 关键点连新建汇点 流量1 源点1 原图中的边 二分流量. 二分+判满流 做完了. 附代码. #include<cstdio> #include<cstri ...

  5. Spoj-NETADMIN Smart Network Administrator

    The citizens of a small village are tired of being the only inhabitants around without a connection ...

  6. SPOJ287 Smart Network Administrator(最大流)

    题目大概是说,一个村庄有n间房子,房子间有m条双向路相连.1号房子有网络,有k间房子要通过与1号房子相连联网,且一条路上不能有同样颜色的线缆,问最少要用几种颜色的线缆. 二分枚举颜色个数,建立容量网络 ...

  7. 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 ...

  8. hihoCoder 1389 Sewage Treatment 【二分+网络流+优化】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)

    #1389 : Sewage Treatment 时间限制:2000ms 单点时限:2000ms 内存限制:256MB 描述 After years of suffering, people coul ...

  9. POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)

    Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9658   Accepted: ...

随机推荐

  1. SNMP协议介绍

    SNMP协议介绍 一.什么是SNMP SNMP:“简单网络管理协议”,用于网络管理的协议.SNMP用于网络设备的管理.SNMP的工作方式:管理员需要向设备获取数据,所以SNMP提供了 “读”操作:管理 ...

  2. Mybatis缓存机制及mybatis的各个组成部分

    Mybatis 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Session,当 Session flush 或 close 之后,该Session中的所有 ...

  3. BZOJ day2

    十六题...(好难啊) 1051105910881191119214321876195119682242243824562463276128184720

  4. 【BZOJ3674】可持久化并查集加强版

    可持久化并查集我觉得就是可持久化数组的一种应用.可持久化数组,顾名思义,就是有历史版本的数组,那么如果我们暴力修改储存的话,修改O(n)查询O(1),空间O(n*m),这样肯定不可行,那么我们发现主席 ...

  5. 旋转数组 [ LeetCode ]

    原题地址:https://leetcode-cn.com/problems/rotate-array/description/ 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. ...

  6. zigbee芯片 - JN5169

    http://www.nxp.com/products/wireless-connectivity/2.4-ghz-wireless-solutions/support-resources-for-j ...

  7. Web.xml过滤器配置及执行顺序概念

    第一个过滤器 @Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain ch ...

  8. [POJ3237]Tree解题报告|树链剖分|边剖

    关于边剖 之前做的大多是点剖,其实转换到边剖非常简单. 我的做法是每个点的点权记录其到父亲节点的边的边权. 只要solve的时候不要把最上面的点记录在内就可以了. Tree Description Y ...

  9. 用 C# 代码如何实现让你的电脑关机,重启,注销,锁定,休眠,睡眠

    简介 本文讲述了用 C# 代码如何实现让你的电脑关机,重启,注销,锁定,休眠,睡眠. 如何实现 首先,使用 using 语句添加我们需要的命名空间: using System.Diagnostics; ...

  10. js三层引号嵌套

    ··· 参考:https://blog.csdn.net/feiyangbaxia/article/details/49681131 第一层用双引号,第二层转义双引号,第三层单引号