Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 53640   Accepted: 20448

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

 
 
n是点数,m是边数
 
EK算法:
    O(n*m*m)
    效率最低,没有任何优化,直接吧标号法实现。
    算法思想:一直重复搜索增广路径的过程,知道不存在增广路径。而此处对于增广路径的搜索,则采用了邻接矩阵的方法,耗时比较大,使用邻接表会更清晰的表现出标号法的过程。
  1. //312K 0MS C++ 1400B 2014-05-03 17:50:04
  2. //最大流模板题
  3. #include<iostream>
  4. #include<queue>
  5. #define INF 0x7ffffff
  6. #define N 205
  7. using namespace std;
  8. int flow[N],vis[N];
  9. int g[N][N],father[N];
  10. int maxflow,n,m;
  11. int Min(int a,int b)
  12. {
  13. return a<b?a:b;
  14. }
  15. void EK(int s,int e)
  16. {
  17. queue<int>Q;
  18. while(){
  19. memset(vis,,sizeof(vis));
  20. memset(flow,,sizeof(flow));
  21. Q.push(s);
  22. flow[s]=INF;
  23. while(!Q.empty()){
  24. int u=Q.front();
  25. Q.pop();
  26. for(int v=;v<=n;v++){
  27. if(!vis[v] && g[u][v]>){
  28. vis[v]=;
  29. father[v]=u;
  30. Q.push(v);
  31. flow[v]=Min(flow[u],g[u][v]);
  32. }
  33. }
  34. if(flow[e]>){
  35. while(!Q.empty()) Q.pop();
  36. break;
  37. }
  38. }
  39. if(flow[e]==) break;
  40. for(int i=e;i!=s;i=father[i]){
  41. g[father[i]][i]-=flow[e];
  42. g[i][father[i]]+=flow[e];
  43. }
  44. maxflow+=flow[e];
  45. }
  46. }
  47. int main(void)
  48. {
  49. int a,b,c;
  50. while(scanf("%d%d",&m,&n)!=EOF)
  51. {
  52. memset(g,,sizeof(g));
  53. maxflow=;
  54. while(m--){
  55. scanf("%d%d%d",&a,&b,&c);
  56. g[a][b]+=c;
  57. }
  58. EK(,n);
  59. printf("%d\n",maxflow);
  60. }
  61. return ;
  62. }

dinic算法:

O(n*n*m)(如果用bfs实现增广路径的话则是O(n*m*m),好像是另一个算法了)

效率比EK高,因为多了优化。

算法思想:重复搜索增广路径的动作,而判定条件改为直到不能分层为止,能避免很多不必要的过程。(因为偷懒所以用了STL实现队列和邻接表)

  1. //15MS 340K 1496 B G++
  2. #include<iostream>
  3. #include<queue>
  4. #include<vector>
  5. #define N 205
  6. #define INF 0x7ffffff
  7. using namespace std;
  8. struct node{
  9. int v,c;
  10. node(int a,int b){
  11. v=a;c=b;
  12. }
  13. };
  14. int n,m;
  15. vector<node>V[N];
  16. int dis[N];
  17. queue<int>Q;
  18. bool bfs() //分层
  19. {
  20. memset(dis,-,sizeof(dis));
  21. dis[]=;
  22. int u=;
  23. Q.push(u);
  24. while(!Q.empty()){
  25. u=Q.front();
  26. Q.pop();
  27. for(int i=;i<V[u].size();i++){
  28. if(dis[V[u][i].v]==- && V[u][i].c>){
  29. dis[V[u][i].v]=dis[u]+;
  30. Q.push(V[u][i].v);
  31. }
  32. }
  33. }
  34. return dis[n]>=;
  35. }
  36. int dfs(int u,int minn) //增广
  37. {
  38. if(u==n) return minn;
  39. for(int i=;i<V[u].size();i++){
  40. if(dis[V[u][i].v]==dis[u]+ && V[u][i].c>){
  41. int c=V[u][i].c;
  42. minn=minn<c?minn:c;
  43. int ans=dfs(V[u][i].v,minn);
  44. if(ans>){
  45. V[u][i].c-=ans;
  46. return ans;
  47. }
  48. }
  49. }
  50. return ;
  51. }
  52. int dinic()
  53. {
  54. int ans=;
  55. while(bfs()){
  56. ans+=dfs(,INF);
  57. //printf("*%d\n",ans);
  58. }
  59. return ans;
  60. }
  61. int main(void)
  62. {
  63. int u,v,c;
  64. while(scanf("%d%d",&m,&n)!=EOF)
  65. {
  66. for(int i=;i<=n;i++)
  67. V[i].clear();
  68. for(int i=;i<m;i++){
  69. scanf("%d%d%d",&u,&v,&c);
  70. V[u].push_back(node(v,c));
  71. }
  72. printf("%d\n",dinic());
  73. }
  74. return ;
  75. }

ISAP:

O(n*n*m)

算法思想:这个算法看的时间比较长,因为比较难找到合理化的解释,第一次基本是套用别人的模板,现在写一下自己的理解。

算法复杂度上的分析我理解的不是很透彻,因此先借用别人的结果。而ISAP算法就分为几部分,开始先逆向BFS初始化距离标号,然后进入ISAP主函数,和标号法的思想差不多,(1)一直循环查找增广路径;(2)找到增广路径时则进行augment()处理,找不到时则回退retreat()处理;(3)当出现断层或则d[source]>=n时算法结束。

推荐一个网站:http://blog.csdn.net/yuhailin060/article/details/4897608

伪代码:

  1. algorithm Improved-Shortest-Augmenting-Path
  2. f <--
  3. 从终点 t 开始进行一遍反向 BFS 求得所有顶点的起始距离标号 d(i)
  4. i <-- s
  5. while d(s) < n do
  6. if i = t then // 找到增广路
  7. Augment
  8. i <-- s // 从源点 s 开始下次寻找
  9. if Gf 包含从 i 出发的一条允许弧 (i,j)
  10. then Advance(i)
  11. else Retreat(i) // 没有从 i 出发的允许弧则回退
  12. return f
  13.  
  14. procedure Advance(i)
  15. (i,j) 为从 i 出发的一条允许弧
  16. pi(j) <-- i // 保存一条反向路径,为回退时准备
  17. i <-- j // 前进一步,使 j 成为当前结点
  18.  
  19. procedure Retreat(i)
  20. d(i) <-- + min{d(j):(i,j)属于残量网络Gf} // 称为重标号的操作
  21. if i != s
  22. then i <-- pi(i) // 回退一步
  23.  
  24. procedure Augment
  25. pi 中记录为当前找到的增广路 P
  26. delta <-- min{rij:(i,j)属于P}
  27. 沿路径 P 增广 delta 的流量
  28. 更新残量网络 Gf

题目解决的实现:

邻接矩阵:

  1. //15MS 532K 2612 B G++
  2. #include<stdio.h>
  3. #include<string.h>
  4. #define N 202
  5. #define inf 0x7fffffff
  6. int n,m,source,sink;
  7. int g[N][N],f[N][N];
  8. int pi[N]; //增广路径
  9. int curnode[N]; //当前节点,优化循环
  10.  
  11. int queue[N]; //队列
  12.  
  13. int d[N]; //距离标号
  14. int num[N]; //num[i] 为 d[j]=i 的数量,记录是否断层,断层结束算法
  15.  
  16. int bfs() //逆向bfs初始化距离标号
  17. {
  18. int head=,tail=;
  19. for(int i=;i<=n;i++)
  20. num[d[i]=n]++;
  21.  
  22. num[n]--;
  23. d[sink]=;
  24. num[]++;
  25.  
  26. queue[++tail]=sink;
  27.  
  28. while(head!=tail){
  29. int u=queue[++head];
  30.  
  31. for(int i=;i<=n;i++){
  32. if(d[i]<n || g[i][u]==) continue;
  33.  
  34. queue[++tail]=i;
  35.  
  36. num[n]--;
  37. d[i]=d[u]+;
  38. num[d[i]]++;
  39. }
  40. }
  41. return ;
  42. }
  43.  
  44. int augment() //处理增广路径
  45. {
  46. int width=inf;
  47. for(int i=sink,j=pi[i];i!=source;i=j,j=pi[j]){
  48. if(g[j][i]<width) width=g[j][i];
  49. }
  50.  
  51. for(int i=sink,j=pi[i];i!=source;i=j,j=pi[j]){
  52. g[j][i]-=width; f[j][i]+=width;
  53. g[i][j]+=width; f[i][j]-=width;
  54. }
  55.  
  56. return width;
  57. }
  58.  
  59. int retreat(int &i) //处理回退,重标号
  60. {
  61. int temp;
  62. int mind=n-;
  63. for(int j=;j<=n;j++)
  64. if(g[i][j]> && d[j]<mind)
  65. mind=d[j];
  66.  
  67. temp=d[i];
  68.  
  69. num[d[i]]--;
  70. d[i]=mind+;
  71. num[d[i]]++;
  72.  
  73. if(i!=source) i=pi[i];
  74.  
  75. return num[temp];
  76. }
  77.  
  78. int maxflow(int s,int e) //ISAP求最大流
  79. {
  80. int i,j,flow=;
  81.  
  82. source=s;
  83. sink=e;
  84. bfs();
  85. //for(int i=1;i<=n;i++) printf("*%d\n",d[i]);
  86. for(i=;i<=n;i++) curnode[i]=;
  87.  
  88. i=source;
  89.  
  90. for( ; d[source]<n ; ){ //主循环
  91. for(j=curnode[i];j<=n;j++) //查找允许弧
  92. if(g[i][j]> && d[i]==d[j]+)
  93. break;
  94. if(j<=n){ //存在允许弧
  95. curnode[i]=j;
  96. pi[j]=i;
  97. i=j;
  98.  
  99. if(i==sink){ //找到增广路径
  100. flow+=augment();
  101. //printf("*%d %d\n",pi[i],flow);
  102. i=source;
  103. }
  104.  
  105. }else{ //不存在允许弧
  106. curnode[i]=;
  107. if(retreat(i)==) break;
  108. }
  109. }
  110. return flow;
  111. }
  112.  
  113. int main(void)
  114. {
  115. int a,b,c;
  116. while(scanf("%d%d",&m,&n)!=EOF)
  117. {
  118. memset(g,,sizeof(g));
  119. memset(f,,sizeof(f));
  120. for(int i=;i<m;i++){
  121. scanf("%d%d%d",&a,&b,&c);
  122. g[a][b]+=c;
  123. }
  124. printf("%d\n",maxflow(,n));
  125. }
  126. return ;
  127. }

邻接表:

  1. //15MS 320K 1811 B G++
  2. #include<iostream>
  3. #define N 1005
  4. #define inf 0x7fffffff
  5. using namespace std;
  6. struct node{
  7. int v;
  8. int c;
  9. int next;
  10. }edge[N];
  11.  
  12. int head[N],eid;
  13. int n,m;
  14.  
  15. int h[N]; //距离标号
  16. int gap[N]; //记录是否断层
  17.  
  18. void addedge(int u,int v,int c) //有向图
  19. {
  20. edge[eid].v=v;
  21. edge[eid].c=c;
  22. edge[eid].next=head[u];
  23. head[u]=eid++;
  24. edge[eid].v=u;
  25. edge[eid].c=;
  26. //edge[eid].c=c; //有向图
  27. edge[eid].next=head[v];
  28. head[v]=eid++;
  29. }
  30. int dfs(int u,int tc)
  31. {
  32. if(u==n) return tc; //找到增广路径
  33. int c0;
  34. int minh=n-;
  35. int temp=tc;
  36. for(int i=head[u];i!=-;i=edge[i].next){ //遍历与u有关的弧
  37. int v=edge[i].v;
  38. int c=edge[i].c;
  39. if(c>){
  40. if(h[v]+==h[u]){ //可行弧
  41. c0=temp<c?temp:c;
  42. c0=dfs(v,c0);
  43. edge[i].c-=c0;
  44. edge[i^].c+=c0;
  45. temp-=c0;
  46. if(h[]>=n) return tc-temp;
  47. if(temp==) break;
  48. }
  49. if(h[v]<minh) minh=h[v]; //更新最短距离标号
  50. }
  51. }
  52. if(temp==tc){ //不存在可行弧,回退操作
  53. --gap[h[u]];
  54. if(gap[h[u]]==) h[]=n; //出现断层
  55. h[u]=minh+;
  56. ++gap[h[u]];
  57. }
  58. return tc-temp;
  59. }
  60. int ISAP() //isap算法
  61. {
  62. int ans=;
  63. memset(gap,,sizeof(gap));
  64. memset(h,,sizeof(h));
  65. gap[]=n;
  66. while(h[]<n){
  67. ans+=dfs(,inf);
  68. }
  69. return ans;
  70. }
  71. int main(void)
  72. {
  73. int u,v,c;
  74. while(scanf("%d%d",&m,&n)!=EOF)
  75. {
  76. memset(head,-,sizeof(head));
  77. eid=;
  78. for(int i=;i<m;i++){
  79. scanf("%d%d%d",&u,&v,&c);
  80. addedge(u,v,c);
  81. }
  82. printf("%d\n",ISAP());
  83. }
  84. return ;
  85. }

poj 1273 && hdu 1532 Drainage Ditches (网络最大流)的更多相关文章

  1. POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)

    Drainage DitchesHal Burch Time Limit 1000 ms Memory Limit 65536 kb description Every time it rains o ...

  2. hdu 1532 Drainage Ditches(最大流模板题)

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. 【初识——最大流】 hdu 1532 Drainage Ditches(最大流) USACO 93

    最大流首次体验感受—— 什么是最大流呢? 从一个出发点(源点),走到一个目标点(汇点),途中可以经过若干条路,每条路有一个权值,表示这条路可以通过的最大流量. 最大流就是从源点到汇点,可以通过的最大流 ...

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

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

  5. hdu 1532 Drainage Ditches (最大流)

    最大流的第一道题,刚开始学这玩意儿,感觉好难啊!哎····· 希望慢慢地能够理解一点吧! #include<stdio.h> #include<string.h> #inclu ...

  6. HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. HDU 1532 Drainage Ditches (最大网络流)

    Drainage Ditches Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) To ...

  8. HDU 1532 Drainage Ditches (网络流)

    A - Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  9. hdu 1532 Drainage Ditches(最大流)

                                                                                            Drainage Dit ...

随机推荐

  1. mysql表的核心元数据

    索引的 mysql> show indexes from recordsInRangeTest; +--------------------+------------+------------- ...

  2. EF Core注意事项

    流程:https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db 1.Both Entity Framework 6. ...

  3. Python拼接字符串的7种方法

    1.直接通过+操作: s = 'Python'+','+'你好'+'!'print(s) 打印结果: Python,你好! 2.通过join()方法拼接: 将列表转换成字符串 strlist=['Py ...

  4. Python入门编程中的变量、字符串以及数据类型

    //2018.10.10 字符串与变量 1. 在输出语句中如果需要出现单引号或者双引号,可以使用转义符号\,它可以将其中的歧义错误解释化解,使得输出正常: 2. 对于python的任何变量都需要进行赋 ...

  5. 教你一招,提升你Python代码的可读性,小技巧

    Python的初学者,开发者都应该知道的代码可读性提高技巧,本篇主要介绍了如下内容: PEP 8是什么以及它存在的原因 为什么你应该编写符合PEP 8标准的代码 如何编写符合PEP 8的代码 为什么我 ...

  6. python切片技巧

    写一个程序,打印数字1到100,3的倍数打印“Fizz”来替换这个数,5的倍数打印“Buzz”,对于既是3的倍数又是5的倍数的数字打印“FizzBuzz” for x in range(101): p ...

  7. lintcode 二叉树中序遍历

    /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * Tr ...

  8. kosaraju求强连通分量

    在了解kosaraju算法之前我们先了解一下什么是强连通分量,在有向图中如果两个定点vi,ui存在一条路劲从vi到达ui且也存在一条路劲从ui到达vi那么由ui和vi这两个点构成的图成为强连通图,简洁 ...

  9. Ext JS 6学习文档-第5章-表格组件(grid)

    Ext JS 6学习文档-第5章-表格组件(grid) 使用 Grid 本章将探索 Ext JS 的高级组件 grid .还将使用它帮助读者建立一个功能齐全的公司目录.本章介绍下列几点主题: 基本的 ...

  10. .net转PHP从零开始-环境的搭建

    PHP初级开发环境安装很简单,只需要使用一键安装的phpstudy 下载地址:http://www.phpstudy.net/ 安装后可以看到 这样的界面,设置好相关的配置,然后,选择查看phpinf ...