Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 70333   Accepted: 27336

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
  2.  
  3. Dinic实现最大流.
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. #include <algorithm>
  5. using namespace std;
  6. const int MAXN=;
  7. const int INF=0x3f3f3f3f;
  8. int arc[MAXN][MAXN];
  9. int n,m;
  10. int level[MAXN];
  11. bool bfs(int src,int ter)
  12. {
  13. memset(level,-,sizeof(level));
  14. queue<int> que;
  15. que.push(src);
  16. level[src]=;
  17. while(!que.empty())
  18. {
  19. int u=que.front();que.pop();
  20. for(int i=;i<=n;i++)
  21. {
  22. if(level[i]<&&arc[u][i]>)
  23. {
  24. que.push(i);
  25. level[i]=level[u]+;
  26. }
  27. }
  28. }
  29. if(level[ter]>) return true;
  30. else return false;
  31. }
  32.  
  33. int dfs(int u,int ter,int f)
  34. {
  35. if(u==ter) return f;
  36. for(int i=;i<=n;i++)
  37. {
  38. int d;
  39. if(arc[u][i]>&&level[i]==level[u]+&&(d=dfs(i,ter,min(arc[u][i],f))))
  40. {
  41. arc[u][i]-=d;
  42. arc[i][u]+=d;
  43. return d;
  44. }
  45. }
  46. return ;
  47. }
  48. int max_flow(int src,int ter)
  49. {
  50. int ans=;
  51. int d;
  52. while(bfs(src,ter))
  53. {
  54. while(d=dfs(src,ter,INF)) ans+=d;
  55. }
  56. return ans;
  57. }
  58. int main()
  59. {
  60. // freopen("input.in","r",stdin);
  61. while(scanf("%d%d",&m,&n)!=EOF)
  62. {
  63. memset(arc,,sizeof(arc));
  64. for(int i=;i<m;i++)
  65. {
  66. int from,to,w;
  67. scanf("%d%d%d",&from,&to,&w);
  68. arc[from][to]+=w;
  69. }
  70. int res=max_flow(,n);
  71. printf("%d\n",res);
  72. }
  73. return ;
  74. }

POJ1273(最大流入门)的更多相关文章

  1. 使用Guava RateLimiter限流入门到深入

    前言 在开发高并发系统时有三把利器用来保护系统:缓存.降级和限流 缓存: 缓存的目的是提升系统访问速度和增大系统处理容量 降级: 降级是当服务出现问题或者影响到核心流程时,需要暂时屏蔽掉,待高峰或者问 ...

  2. JavaIo流入门篇之字节流基本使用。

    一 基本知识了解(  字节流, 字符流, byte,bit是啥?) /* java中字节流和字符流之前有接触过,但是一直没有深入的学习和了解. 今天带着几个问题,简单的使用字节流的基本操作. 1 什么 ...

  3. Java-io流入门到精通详细总结

    IO流:★★★★★,用于处理设备上数据. 流:可以理解数据的流动,就是一个数据流.IO流最终要以对象来体现,对象都存在IO包中. 流也进行分类: 1:输入流(读)和输出流(写). 2:因为处理的数据不 ...

  4. IO流入门-第十三章-File相关

    /* java.io.File 1.File和流无关,不能通过该类完成文件的读写 2.File是文件和目录路径名的抽象变现形式. */ import java.io.*; public class F ...

  5. IO流入门-第十二章-ObjectInputStream_ObjectOutputStream

    DataInputStream和DataOutputStream基本用法和方法示例,序列化和反序列化 import java.io.Serializable; //该接口是一个“可序列化”的 ,没有任 ...

  6. IO流入门-第十一章-PrintStream_PrintWriter

    DataInputStream和DataOutputStream基本用法和方法示例 /* java.io.PrintStream:标准的输出流,默认打印到控制台,以字节方式 java.io.Print ...

  7. IO流入门-第十章-DataInputStream_DataOutputStream

    DataInputStream和DataOutputStream基本用法和方法示例 /* java.io.DataOutputStream 数据字节输出流,带着类型写入 可以将内存中的“int i = ...

  8. IO流入门-第九章-BufferedReader_BufferedWriter复制

    利用BufferedReader和BufferedWriter进行复制粘贴 import java.io.*; public class BufferedReader_BufferedWriterCo ...

  9. IO流入门-第八章-BufferedWriter

    BufferedWriter基本用法和方法示例 import java.io.*; public class BufferedWriterTest01 { public static void mai ...

  10. IO流入门-第七章-BufferedReader

    BufferedReader基本用法和方法示例 /* 字节 BufferedInputStream BufferedOutputStream 字符 BufferedReader:带有缓冲区的字符输入流 ...

随机推荐

  1. ZOJ 2975 Kinds of Fuwas(暴力+排列组合)

    Kinds of Fuwas Time Limit: 2 Seconds      Memory Limit: 65536 KB In the year 2008, the 29th Olympic ...

  2. (转)MapReduce Design Patterns(chapter 2 (part 2))(三)

    Median and standard deviation 中值和标准差的计算比前面的例子复杂一点.因为这种运算是非关联的,它们不是那么容易的能从combiner中获益.中值是将数据集一分为两等份的数 ...

  3. flowable ProcessEngine和ProcessEngineConfiguration

    ProcessEngine是流程引擎,ProcessEngineConfiguration与前面四个引擎配置有些不同. ProcessEngineConfiguration增加了邮件服务和httpCl ...

  4. 单机ZooKeeper配置

    1.创建zoo.cfg copy D:\zookeeper3.4.6\conf\zoo_sample.cfg zoo.cfg 修改追加如下内容 dataDir=D:/zookeeper3.4.6/da ...

  5. caffe 细节

    batch :http://www.zhihu.com/question/32673260 caffe blog: http://blog.csdn.net/abcjennifer/article/d ...

  6. python类中self是什么

    参考文献:http://www.cnblogs.com/linuxcat/archive/2012/01/05/2220997.html 注: (1)self在定义类的方法时是必须有的. (2)调用时 ...

  7. 【Python爬虫学习笔记(2)】正则表达式(re模块)相关知识点总结

    1. 正则表达式 正则表达式是可以匹配文本片段的模式. 1.1 通配符 正则表达式能够匹配对于一个的字符串,可以使用特殊字符创建这类模式.(图片来自cnblogs) 1.2 特殊字符的转义 由于在正则 ...

  8. 神器如 dnSpy,无需源码也能修改 .NET 程序

    dnSpy 是 0xd4d 开发的 .NET 程序调试神器. 说它是神器真的毫不为过!它能在完全没有源码的情况下即时调试程序,甚至还能修改程序!本文讲向大家介绍如何使用 dnSpy 修改 .NET 程 ...

  9. 用firefox获取html页面元素的Xpath

    Xpath在分析网页尤其是采集固定格式数据时,非常有用,且比正则表达式和首尾截取式更加简便.准确! 工具/原料   FireFox FireBug XpathChecker UserAgentSwit ...

  10. hadoop入门手册1:hadoop【2.7.1】【多节点】集群配置【必知配置知识1】

    问题导读 1.说说你对集群配置的认识?2.集群配置的配置项你了解多少?3.下面内容让你对集群的配置有了什么新的认识? 目的 目的1:这个文档描述了如何安装配置hadoop集群,从几个节点到上千节点.为 ...