Drainage Ditches

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 67823   Accepted: 26209

Description

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 

Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input

  1. 5 4
  2. 1 2 40
  3. 1 4 20
  4. 2 4 20
  5. 2 3 30
  6. 3 4 10

Sample Output

  1. 50

Source

——————————我是分割线——————————————————————————————————————————————————
水题,模板题。
网络流,最大流。
增广路算法求解。
我竟然调了两个钟头......
正解是DINIC增广,但我直接BFS增广竟然也过了……
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<cassert>
  7. #include<climits>
  8. #define maxn 210
  9. using namespace std;
  10. void find();
  11. void flow();
  12. void update();
  13. struct Edge
  14. {
  15. int c;
  16. int f;
  17. }edge[maxn][maxn];
  18. int n,m;
  19. int s,t;
  20. int residual[maxn][maxn];
  21. int que[maxn*maxn],head,tail;
  22. int pre[maxn];
  23. bool vis[maxn];
  24. int max_flow,min_flow;
  25. void find()
  26. {
  27. int i,cu;
  28. memset(vis,false,sizeof(vis));
  29. memset(residual,,sizeof(residual));
  30. memset(pre,,sizeof(pre));
  31. head=;que[head]=s;pre[s]=s;vis[s]=true;tail=;
  32. while(head<tail&&pre[t]==)
  33. {
  34. cu=que[head];
  35. for(i=;i<=n;i++)
  36. {
  37. if(vis[i]==false)
  38. {
  39. if(edge[cu][i].c-edge[cu][i].f>)
  40. {
  41. residual[cu][i]=edge[cu][i].c-edge[cu][i].f;
  42. pre[i]=cu;que[tail++]=i;vis[i]=true;
  43. }
  44. else if(edge[i][cu].f>)
  45. {
  46. residual[cu][i]=edge[i][cu].f;
  47. pre[i]=cu;que[tail++]=i;vis[i]=true;
  48. }
  49. }
  50. }
  51. head++;
  52. }
  53. }
  54. void flow()
  55. {
  56. int i=t,j;
  57. if(pre[i]==)
  58. {
  59. min_flow=;return;
  60. }
  61. j=0x7fffffff;
  62. while(i!=s)
  63. {
  64. if(residual[pre[i]][i]<j) j=residual[pre[i]][i];
  65. i=pre[i];
  66. }
  67. min_flow=j;
  68. }
  69. void update()
  70. {
  71. int i=t;
  72. if(pre[i]==) return;
  73. while(i!=s)
  74. {
  75. if(edge[pre[i]][i].c-edge[pre[i]][i].f>)
  76. edge[pre[i]][i].f+=min_flow;
  77. else if(edge[i][pre[i]].f>)
  78. edge[pre[i]][i].f+=min_flow;
  79. i=pre[i];
  80. }
  81. }
  82. void solve()
  83. {
  84. s=;t=n;
  85. max_flow=;
  86. while(true)
  87. {
  88. find();flow();
  89. max_flow+=min_flow;
  90. if(min_flow>) update();
  91. else return;
  92. }
  93. }
  94. int main()
  95. {
  96. std::ios::sync_with_stdio(false);
  97. int i,u,v,c;
  98. while(scanf("%d %d",&m,&n)!=EOF)
  99. {
  100. memset(edge,,sizeof(edge));
  101. for(i=;i<m;i++)
  102. {
  103. scanf("%d %d %d",&u,&v,&c);
  104. edge[u][v].c+=c;
  105. }
  106. solve();
  107. printf("%d\n",max_flow);
  108. }
  109. return ;
  110. }

POJ 1273 Drainage Ditches题解——S.B.S.的更多相关文章

  1. poj 1273 Drainage Ditches(最大流)

    http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  2. POJ 1273 Drainage Ditches (网络最大流)

    http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  3. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

  4. poj 1273 Drainage Ditches 最大流入门题

    题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...

  5. poj 1273 Drainage Ditches(最大流,E-K算法)

    一.Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clove ...

  6. POJ 1273 Drainage Ditches

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67387   Accepted: 2603 ...

  7. POJ 1273 Drainage Ditches -dinic

    dinic版本 感觉dinic算法好帅,比Edmonds-Karp算法不知高到哪里去了 Description Every time it rains on Farmer John's fields, ...

  8. Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )

    题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...

  9. POJ 1273 Drainage Ditches(网络流dinic算法模板)

    POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...

随机推荐

  1. Z.ExtensionMethods 一个强大的开源扩展库

    今天有意的在博客园里面搜索了一下 Z.ExtensionMethods 这个扩展类库,确发现只搜到跟这个真正相关的才两篇博文而已,我都点进去看了一下,也都只是提到而已,没有专门介绍,才引起我写这篇文档 ...

  2. C# DataGridView中指定的单元格不能编辑

    注意:DataGridView控件是从.NET Framework 2.0版本开始追加的. ReadOnly属性的使用 DataGridView内所有的单元格不能编辑 当DataGridView.Re ...

  3. mybatis报错invalid types () or values ()解决方法

      原因: Pojo类User没提供无参数构造方法, 加上该构造方法后,问题解决 ### Cause: org.apache.ibatis.reflection.ReflectionException ...

  4. Mac上编译C++报错

    今天在使用Mac编译C++文件时,提示以下错误. Undefined symbols for architecture x86_64: "std::__1::__vector_base_co ...

  5. karma与webpack结合

    一.必备插件 1.babel:es6的语法支持 2.karma:测试框架 3.jasmine:断言框架 4.webpack:打包工具 5.karma-webpack:karma调用webpack打包接 ...

  6. Egret白鹭H5小游戏开发入门(二)

    前言: 昨天的文章中简单的介绍了Egret白鹭引擎从安装到基本的使用配置等问题,今天着重介绍H5小游戏开发的起步阶段,如Wing面板的使用,素材的处理,类的说明,开始布局等等. 整体概况: 根据上一篇 ...

  7. 完美 全兼容 解决 文字两端对齐 justify 中文姓名对齐

    text-align:justify; 所有浏览器都支持,text-justify之类的却只有IE支持,就不要考虑了. justify我的理解,使元素内部的子元素两端对齐,子元素当然只能是inline ...

  8. 那些过目不忘的H5页面

    原文链接:http://isux.tencent.com/great-mobile-h5-pages.html 从引爆朋友圈的H5小游戏<围住神经猫>,到颠覆传统广告的大众点评H5专题页& ...

  9. 详解JavaScript中的this

    JavaScript中的this总是让人迷惑,应该是js众所周知的坑之一. 个人也觉得js中的this不是一个好的设计,由于this晚绑定的特性,它可以是全局对象,当前对象,或者…有人甚至因为坑大而不 ...

  10. iOS GCD的 一次性执行、定时器、迭代、队列组