1. #include<iostream>
  2. /*
  3. 题意:就是寻找从源点到汇点的最大流!
  4. 要注意的是每两个点的流量可能有多个,也就是说有重边,所以要把两个点的所有的流量都加起来
  5. 就是这两个点之间的流量了!
  6.  
  7. 思路:建图之后直接套用最大流算法(EK, 或者是Dinic算法) 图解Dinic算法流程!

  8. */
  9. #include<queue>
  10. #include<cstring>
  11. #include<cstdio>
  12. #include<algorithm>
  13. #define INF 0x3f3f3f3f3f3f3f3f
  14. #define N 205
  15. using namespace std;
  16. typedef long long LL;
  17. LL cap[N][N];
  18.  
  19. int m, n;
  20. LL maxFlow;
  21. int d[N];
  22. queue<int>q;
  23.  
  24. bool bfs(){
  25. q.push();
  26. memset(d, , sizeof(d));
  27. d[]=;
  28. while(!q.empty()){
  29. int u=q.front();
  30. q.pop();
  31. for(int v=; v<=n; ++v)
  32. if(!d[v] && cap[u][v]>){
  33. d[v]=d[u]+;
  34. q.push(v);
  35. }
  36. }
  37. if(!d[n]) return false;
  38. return true;
  39. }
  40.  
  41. LL dfs(int u, LL flow){
  42. if(u==n) return flow;
  43. for(int v=; v<=n; ++v)
  44. if(d[v]==d[u]+ && cap[u][v]>){
  45. LL a=dfs(v, min(flow, cap[u][v]));
  46. if(a==) continue;//如果a==0 说明没有找到从起点到汇点的增广路, 然后换其他路接着寻找!
  47. cap[u][v]-=a;
  48. cap[v][u]+=a;
  49. return a;
  50. }
  51. return ;
  52. }
  53.  
  54. void Dinic(){
  55. LL flow;
  56. while(bfs()){//利用bfs构造好层次图,这样dfs在寻找阻塞流的时候,就不会盲目的寻找了!
  57. while(flow=dfs(, INF)) maxFlow+=flow;//利用构造好的层次图不断的寻找阻塞流!
  58. }
  59. }
  60.  
  61. int main(){
  62. while(scanf("%d%d", &m, &n)!=EOF){
  63. memset(cap, , sizeof(cap));
  64. while(m--){
  65. int u, v;
  66. LL w;
  67. scanf("%d%d%lld", &u, &v, &w);
  68. cap[u][v]+=w;
  69. }
  70. maxFlow=;
  71. Dinic();
  72. printf("%lld\n", maxFlow);
  73. }
  74. return ;
  75. }
  1. //EK算法同样搞定
  2. #include<iostream>
  3. #include<queue>
  4. #include<cstdio>
  5. #include<cstring>
  6. #include<algorithm>
  7. #define INF 0x3f3f3f3f
  8. using namespace std;
  9. typedef __int64 LL;
  10. LL cap[][];
  11. int pre[];
  12. LL a[];
  13. int m, n;
  14. queue<int>q;
  15. LL maxFlow;
  16. bool spfa(){
  17. while(!q.empty()) q.pop();
  18. memset(a, , sizeof(a));
  19. q.push();
  20. a[]=INF;
  21. while(!q.empty()){
  22. int u=q.front();
  23. q.pop();
  24. for(int v=; v<=n; ++v)
  25. if(!a[v] && cap[u][v]>){
  26. pre[v]=u;
  27. a[v]=min(a[u], cap[u][v]);
  28. q.push(v);
  29. }
  30. if(a[n]) break;
  31. }
  32. if(!a[n]) return false;
  33. return true;
  34. }
  35.  
  36. void EK(){
  37. maxFlow=;
  38. while(spfa()){
  39. int u=n;
  40. maxFlow+=a[n];
  41. while(u!=){
  42. cap[pre[u]][u]-=a[n];
  43. cap[u][pre[u]]+=a[n];
  44. u=pre[u];
  45. }
  46. }
  47. }
  48. int main(){
  49. while(scanf("%d%d", &m, &n)!=EOF){
  50. memset(cap, , sizeof(cap));
  51. while(m--){
  52. int u, v;
  53. LL w;
  54. scanf("%d%d%I64d", &u, &v, &w);
  55. cap[u][v]+=w;
  56. }
  57. EK();
  58. printf("%I64d\n", maxFlow);
  59. }
  60. return ;
  61. }

poj1273Drainage Ditches的更多相关文章

  1. POJ1273Drainage Ditches[最大流]

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 71559   Accepted: 2784 ...

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

    Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This ...

  3. POJ-1273-Drainage Ditches 朴素增广路

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 70588   Accepted: 2743 ...

  4. 图论-网络流-最大流--POJ1273Drainage Ditches(Dinic)

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 91585   Accepted: 3549 ...

  5. POJ-1273Drainage Ditches(网络流入门题,最大流)

    Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This ...

  6. poj1273--Drainage Ditches(最大流Edmond-Karp算法 邻接表实现)

    最大流模板题 大部分Edmond-Karp算法代码都是邻接矩阵实现,试着改成了邻接表. #include <iostream> #include <cstdio> #inclu ...

  7. 【最大流Dinic模板】HDU1532&POJ1273-Drainage Ditches(16/3/6更正)

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...

  8. POJ 1273 Drainage Ditches题解——S.B.S.

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67823   Accepted: 2620 ...

  9. I:trainage Ditches

    总时间限制: 1000ms 内存限制: 65536kB描述Every time it rains on Farmer John's fields, a pond forms over Bessie's ...

随机推荐

  1. HttpServletRequest的Attribute和Parameter区别

    HttpServletRequest类既有getAttribute()方法,也由getParameter()方法,这两个方法有以下的组件通过getParameter()方法来获得请求参数,例如假定we ...

  2. [原] XAF How to bind a stored procedure to a ListView in XAF

    First, I suggest that you review the following topic to learn how to show a custom set of objects in ...

  3. Python-变量

    1.Python的变量是什么 变量是用来存储计算机程序中的信息,唯一的目的是将数据存储在内存中. 2.Python变量的组成 变量由字母.数字.下划线组成: 变量的第一位不能是数字,可以是字母或下划线 ...

  4. 关于Apache日志的统计

    统计apache日志文件里访问量前十的ip并按从多到少排列 五月 31, 2012 by FandLR   Filed under Linux Leave a comment 解法1: cat acc ...

  5. spring 3.1 配置 JCR 303 Bean Validation

    A) 导入Hibernate-Validator  要使用JSR303 校验框架, 需要加入框架的具体实现Hibernate-Validator, 在soureforge上下载最新的Hibernate ...

  6. c一些关键字

    register:这个关键字请求编译器尽可能的将变量存在CPU内部寄存器中,而不是通过内存寻址访问,以提高效率.注意是尽可能,不是绝对. extern:可以置于变量或者函数前,以标示变量或者函数的定义 ...

  7. Quartz.net2.2初体验

    简介:Quartz.net是一个开源的作用调度框架,非常强大,能够通过简单的配置帮助我们定时具体的操作.相对于我们用的线程里面while(true)然后sleep来执行某个操作,应该算的上是高端,大气 ...

  8. C# 调用restful服务开源库

    .NET环境下我们想调用其它开放平台的服务接口,不需要自己去实现底层,开源的库用起来会很方便 hammock http://www.cnblogs.com/shanyou/archive/2012/0 ...

  9. Xamarin.IOS之多视图

    欢迎大家加入以下开源社区 Xamarin-Cn:https://github.com/Xamarin-Cn Mvvmcross-Cn:https://github.com/Mvvmcross-Cn  ...

  10. 我的ORM之十三 -- 性能参数

    我的ORM索引 测试环境 台式机: 主板:映泰Z77 CPU:i5 3470(3.2GHz) 内存:DDR3 1600 8G(单条) 硬盘:创见 SSD 256G ORM从过程上,可以分两个大的部分: ...