2015 ACM / ICPC 北京站 热身赛 C题

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<queue>
  5. #include<vector>
  6. #include<algorithm>
  7. using namespace std;
  8.  
  9. const int INF=0x7FFFFFFF;
  10. const int maxn=+;//点的数量
  11. int n,m,k;
  12. int u[+],v[+];
  13. int dis1[maxn],dis2[maxn];
  14. bool flag1[maxn],flag2[maxn];
  15. vector<int>P[maxn];
  16. vector<int>FP[maxn];
  17. queue<int>Q1;
  18. queue<int>Q2;
  19.  
  20. struct Edge
  21. {
  22. int from, to, cap, flow;
  23. Edge(int u, int v, int c, int f) :from(u), to(v), cap(c), flow(f) {}
  24. };
  25. vector<Edge>edges;
  26. vector<int>G[maxn*];
  27. bool vis[maxn*];
  28. int d[maxn*];
  29. int cur[maxn*];
  30. int s, t;
  31.  
  32. void read()
  33. {
  34. for(int i=;i<=m;i++)
  35. {
  36. scanf("%d%d",&u[i],&v[i]);
  37. P[u[i]].push_back(v[i]);
  38. FP[v[i]].push_back(u[i]);
  39. }
  40. }
  41.  
  42. void init()
  43. {
  44. for(int i=;i<maxn;i++) P[i].clear();
  45. for(int i=;i<maxn;i++) FP[i].clear();
  46.  
  47. for(int i=;i<=n;i++) dis1[i]=INF,dis2[i]=INF;
  48.  
  49. for(int i = ; i < *maxn; i++) G[i].clear();
  50. edges.clear();
  51. }
  52.  
  53. void AddEdge(int from, int to, int cap)
  54. {
  55. edges.push_back(Edge(from, to, cap, ));
  56. edges.push_back(Edge(to, from, , ));
  57. int w = edges.size();
  58. G[from].push_back(w - );
  59. G[to].push_back(w - );
  60. }
  61.  
  62. bool BFS()
  63. {
  64. memset(vis, , sizeof(vis));
  65. queue<int>Q;
  66. Q.push(s);
  67. d[s] = ;
  68. vis[s] = ;
  69. while (!Q.empty())
  70. {
  71. int x = Q.front();
  72. Q.pop();
  73. for (int i = ; i<G[x].size(); i++)
  74. {
  75. Edge e = edges[G[x][i]];
  76. if (!vis[e.to] && e.cap>e.flow)
  77. {
  78. vis[e.to] = ;
  79. d[e.to] = d[x] + ;
  80. Q.push(e.to);
  81. }
  82. }
  83. }
  84. return vis[t];
  85. }
  86.  
  87. int DFS(int x, int a)
  88. {
  89. if (x == t || a == )
  90. return a;
  91. int flow = , f;
  92. for (int &i = cur[x]; i<G[x].size(); i++)
  93. {
  94. Edge e = edges[G[x][i]];
  95. if (d[x]+ == d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>)
  96. {
  97. edges[G[x][i]].flow+=f;
  98. edges[G[x][i] ^ ].flow-=f;
  99. flow+=f;
  100. a-=f;
  101. if(a==) break;
  102. }
  103. }
  104. if(!flow) d[x] = -;
  105. return flow;
  106. }
  107.  
  108. int dinic(int s, int t)
  109. {
  110. int flow = ;
  111. while (BFS())
  112. {
  113. memset(cur, , sizeof(cur));
  114. flow += DFS(s, INF);
  115. }
  116. return flow;
  117. }
  118.  
  119. void spfa1(int x)
  120. {
  121. while(!Q1.empty()) Q1.pop();
  122. memset(flag1,,sizeof flag1);
  123.  
  124. dis1[x]=;
  125. flag1[x]=;
  126. Q1.push(x);
  127.  
  128. while(!Q1.empty())
  129. {
  130. int h=Q1.front(); flag1[h]=; Q1.pop();
  131. for(int i=;i<P[h].size();i++)
  132. {
  133. if(dis1[h]+<dis1[P[h][i]])
  134. {
  135. dis1[P[h][i]]=dis1[h]+;
  136. if(flag1[P[h][i]]==)
  137. {
  138. flag1[P[h][i]]=;
  139. Q1.push(P[h][i]);
  140. }
  141. }
  142. }
  143. }
  144. }
  145.  
  146. void spfa2(int x)
  147. {
  148. while(!Q2.empty()) Q2.pop();
  149. memset(flag2,,sizeof flag2);
  150.  
  151. dis2[x]=;
  152. flag2[x]=;
  153. Q2.push(x);
  154.  
  155. while(!Q2.empty())
  156. {
  157. int h=Q2.front(); flag2[h]=; Q2.pop();
  158. for(int i=;i<FP[h].size();i++)
  159. {
  160. if(dis2[h]+<dis2[FP[h][i]])
  161. {
  162. dis2[FP[h][i]]=dis2[h]+;
  163. if(flag2[FP[h][i]]==)
  164. {
  165. flag2[FP[h][i]]=;
  166. Q2.push(FP[h][i]);
  167. }
  168. }
  169. }
  170. }
  171. }
  172.  
  173. int main()
  174. {
  175. while(~scanf("%d%d%d",&n,&m,&k))
  176. {
  177. if(!n&&!m&&!k) break;
  178.  
  179. init();
  180. read();
  181.  
  182. spfa1();
  183. spfa2(n);
  184.  
  185. s=;t=n+n;
  186.  
  187. for(int i=;i<=n;i++) AddEdge(i+n,i,);
  188.  
  189. for(int i=;i<=m;i++)
  190. if(dis1[u[i]]+dis2[v[i]]+<=k)
  191. AddEdge(u[i],v[i]+n,INF);
  192.  
  193. int ans=dinic(s,t);
  194. printf("%d\n",ans);
  195. }
  196. return ;
  197. }

HDU 2485 Destroying the bus stations的更多相关文章

  1. HDU 2485 Destroying the bus stations(!最大流∩!费用流∩搜索)

    Description Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “im ...

  2. 图论--网络流--最小割 HDU 2485 Destroying the bus stations(最短路+限流建图)

    Problem Description Gabiluso is one of the greatest spies in his country. Now he's trying to complet ...

  3. HDU 2485 Destroying the bus stations (IDA*+ BFS)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意:给你n个点,m条相连的边,问你最少去掉几个点使从1到n最小路径>=k,其中不能去掉1, ...

  4. HDU 2485 Destroying the bus stations(费用流)

    http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意: 现在要从起点1到终点n,途中有多个车站,每经过一个车站为1时间,现在要在k时间内到达终点,问至少要 ...

  5. hdu 2485 Destroying the bus stations 最小费用最大流

    题意: 最少需要几个点才能使得有向图中1->n的距离大于k. 分析: 删除某一点的以后,与它相连的所有边都不存在了,相当于点的容量为1.但是在网络流中我们只能直接限制边的容量.所以需要拆点来完成 ...

  6. HDUOJ----2485 Destroying the bus stations(2008北京现场赛A题)

    Destroying the bus stations                                                                          ...

  7. Destroying the bus stations

    Destroying the bus stations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1832   Acce ...

  8. Destroying the bus stations HDU - 2485(最小割点)

    题意: 就是求最小割点 解析: 正向一遍spfa 反向一遍spfa  然后遍历每一条边,对于当前边 如果dis1[u] + dis2[v] + 1 <= k 那么就把这条边加入到网络流图中, 每 ...

  9. POJ 3921 Destroying the bus stations 沿着最短路迭代加深搜索

    题目:给出一个图,问最少删除多少个点,使得从点1到点n经过的点数超过k个. 分析: 上网搜了一下,发现很多人用网络流做的,发现我不会.再后来看到这篇说网络流的做法是错的,囧. 后来发现点数有点少,直接 ...

随机推荐

  1. Chapter 2 Open Book——7

    I gunned my deafening engine to life, ignoring the heads that turned inmy direction, and backed care ...

  2. int与byte的区别

    Java中涉及byte.short和char类型的运算操作首先会把这些值转换为int类型,然后对int类型值进行运算,最后得到int类型的结果.因此,如果把两个byte类型值相加,最后会得到一个int ...

  3. hdu_5695_Gym Class(拓扑排序)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5695 题意:中文题,不解释 题解:逆向拓扑字典序就行 #include<cstdio> # ...

  4. token 入门教程

    下载 先去Apache下载一个tomcat 启动 进到安装目录的bin目录运行startup.bat,D:\apache-tomcat-8.0.33\bin (如果双击startup.bat一会自动关 ...

  5. 【转载】CentOS 6.4下PXE+Kickstart无人值守安装操作系统

    [转载]CentOS 6.4下PXE+Kickstart无人值守安装操作系统 转自:CentOS 6.4下PXE+Kickstart无人值守安装操作系统 - David_Tang - 博客园 http ...

  6. 近十年one-to-one最短路算法研究整理【转】

    前言:针对单源最短路算法,目前最经典的思路即标号算法,以Dijkstra算法和Bellman-Ford算法为根本演进了各种优化技术和算法.针对复杂网络,传统的优化思路是在数据结构和双向搜索上做文章,或 ...

  7. First()、FirstOrDefault()、Single() 和 SingleOrDefault()的区别

    Enumerable.First() 方法:返回序列中的第一个元素,如果源序列为空,则抛异常. Enumerable.FirstOrDefault ()方法返回序列中的第一个元素:如果序列中不包含任何 ...

  8. linux学习的哲学层面的思考-架构

    参考:http://blog.chinaunix.net/uid-26119273-id-3356414.html 学习Linux,准备做产品的话,不要把Linux当成了终极目标(当然,这是对应用而言 ...

  9. 【0-1 背包模板】 poj 3624

    先看个未经优化的二维空间dp: #include <iostream> #include <cstdio> #include <cmath> #include &l ...

  10. 如何使用Android中的OpenGL ES媒体效果

    引自:http://www.2cto.com/kf/201506/404366.html Android的媒体效果框架允许开发者可以很容易的应用多种令人印象深刻的视觉效果到照片或视频之上.作为这个媒体 ...