POJ 1273
给出M条边,N个点,求源点1到汇点N的最大流量。

本文主要就是附上dinic的模板,供以后参考。

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <string.h>
  6.  
  7. /*
  8. POJ 1273
  9. dinic算法模板
  10.  
  11. 边是有向的,而且存在重边,且这里重边不是取MAX,而是累加和
  12. */
  13. using namespace std;
  14. const int INF=0x3f3f3f3f;
  15. const int maxn=;
  16. int pri[maxn];
  17. long long sum; //计算总流量
  18. int s,t; //s:源点 t:汇点
  19. int n,m;
  20.  
  21. struct Edge{
  22. int c,f;
  23. }maps[maxn][maxn];
  24.  
  25. int min(int a,int b){
  26. return a<b?a:b;
  27. }
  28. //每次先BFS,看看是否存在从源点到汇点的增广路
  29. bool BFS() {
  30. queue<int> q;
  31. memset(pri,,sizeof(pri));
  32. pri[]=;
  33. q.push();
  34. while(!q.empty()) {
  35. int temp=q.front();
  36. q.pop();
  37. for(int i=; i<=m; i++) {
  38. if(!pri[i] && maps[temp][i].c-maps[temp][i].f){
  39. pri[i]=pri[temp]+;
  40. if(i==t)
  41. return true; //即如果可以流到汇点,直接return true
  42. q.push(i);
  43. }
  44. }
  45. }
  46. //if(pri[m]>0)
  47. // return true;
  48. return false;
  49. }
  50.  
  51. //p表示当前节点,flow表示该节点通过的流量
  52. int dinic(int p,int flow){
  53. if(p==t){
  54. return flow;
  55. }
  56. int f=flow;
  57. //int value=0;
  58. for(int i=;i<=m;i++){
  59. if(pri[i]==pri[p]+ && maps[p][i].c-maps[p][i].f){
  60. int a=maps[p][i].c-maps[p][i].f; //a为该边可以增加的流量
  61. int ff=dinic(i,min(a,flow)); //ff为路径中所有a的最小值,即为该条路中可以增加的流量
  62. maps[p][i].f+=ff; //正向边
  63. maps[i][p].f-=ff; //逆向边
  64. //value+=ff;
  65. flow-=ff;
  66. if(flow<=)
  67. break; //优化剪枝
  68. }
  69. }
  70. //return value;
  71. if(f-flow<=)
  72. pri[p]=;//如果从p点流出去的流量<=0,那么设置pri[p]的值为0,之后在dinic中就不考虑到p点的情况了。
  73. return f-flow;
  74. }
  75. void init(){
  76. for(int i=;i<=m;i++){
  77. for(int j=;j<=m;j++){
  78. maps[i][j].c=maps[i][j].f=;
  79. }
  80. }
  81. }
  82. int main() {
  83. int a,b,c;
  84. s=;
  85. while(scanf("%d%d",&n,&m)!=EOF){
  86. init();
  87. //memset(maps,0,sizeof(maps));
  88. sum=;
  89. t=m;
  90. for(int i=;i<=n;i++){
  91. scanf("%d%d%d",&a,&b,&c);
  92. maps[a][b].c+=c; //该题有重边,这里要+=,不是去max
  93. }
  94. while(BFS()){
  95. sum+=dinic(s,INF);
  96. }
  97. printf("%I64d\n",sum);
  98. }
  99. return ;
  100. }

再给出一个大牛的模板:

  1. #include <cstdio>
  2. #include <queue>
  3.  
  4. using namespace std;
  5.  
  6. typedef int LL;
  7. const int N = ;
  8. const int M = N << ;
  9. const int INF = (int)1e9;
  10.  
  11. struct Dinic {
  12.  
  13. struct Edge {
  14. int v;
  15. LL cap, flow;
  16. Edge* next, * pair;
  17.  
  18. void init(int a, LL b, Edge* e1, Edge* e2) {
  19. v = a, cap = b, flow = , next = e1, pair = e2;
  20. }
  21. };
  22.  
  23. Edge* head[N], * used[N];
  24. Edge* it;
  25. int lev[N], que[N];
  26. Edge E[M];
  27. int n, s, t;
  28. LL maxFlow;
  29.  
  30. void init(int n, int s, int t) {
  31. it = E;
  32. this->n = n;
  33. this->s = s, this->t = t;
  34. for (int i = ; i < n; i++)
  35. head[i] = ;
  36. }
  37.  
  38. void add(int u, int v, LL c) {
  39. it->init(v, c, head[u], it + );
  40. head[u] = it++;
  41. it->init(u, , head[v], it - );
  42. head[v] = it++;
  43. }
  44.  
  45. bool bfs() {
  46. for (int i = ; i < n; lev[i++] = -);
  47. lev[s] = ;
  48. int st = , ed = ;
  49. que[ed++] = s;
  50. while (st < ed) {
  51. int u = que[st++];
  52. for (Edge* e = head[u]; e; e = e->next) {
  53. int v = e->v;
  54. if (lev[v] == - && e->cap > e->flow) {
  55. lev[v] = lev[u] + ;
  56. que[ed++] = v;
  57. }
  58. }
  59. }
  60. return lev[t] != -;
  61. }
  62.  
  63. LL dfs(int u, LL f) {
  64. if (u == t) return f;
  65. for (Edge* & e = used[u]; e; e = e->next) {
  66. int v = e->v;
  67. if (e->cap > e->flow && lev[v] == lev[u] + ) {
  68. LL tmp = dfs(v, min(e->cap - e->flow, f));
  69. if (tmp > ) {
  70. e->flow += tmp;
  71. e->pair->flow -= tmp;
  72. return tmp;
  73. }
  74. }
  75. }
  76. return ;
  77. }
  78.  
  79. void run() {
  80. maxFlow = ;
  81. while (bfs()) {
  82. for (int i = ; i < n; i++)
  83. used[i] = head[i];
  84. LL f = ;
  85. while (f) {
  86. f = dfs(s, INF);
  87. maxFlow += f;
  88. }
  89. }
  90. }
  91.  
  92. }G;
  93.  
  94. int main() {
  95. int n, m, u, v, w;
  96. while (~scanf("%d%d", &m, &n)) {
  97. G.init(n, , n - );
  98. while (m--) {
  99. scanf("%d%d%d", &u, &v, &w);
  100. G.add(u - , v - , w);
  101. }
  102. G.run();
  103. printf("%d\n", G.maxFlow);
  104. }
  105. return ;
  106. }

POJ 1273 Drainage Ditches(网络流dinic算法模板)的更多相关文章

  1. POJ 1273 Drainage Ditches (网络流Dinic模板)

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

  2. poj 1273 Drainage Ditches 网络流最大流基础

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59176   Accepted: 2272 ...

  3. POJ 1273 Drainage Ditches 网络流 FF

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 74480   Accepted: 2895 ...

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

    网络流模板题. ============================================================================================ ...

  5. poj 1273 Drainage Ditches(最大流)

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

  6. POJ 1273 Drainage Ditches

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

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

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

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

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

  9. 网络流最经典的入门题 各种网络流算法都能AC。 poj 1273 Drainage Ditches

    Drainage Ditches 题目抽象:给你m条边u,v,c.   n个定点,源点1,汇点n.求最大流.  最好的入门题,各种算法都可以拿来练习 (1):  一般增广路算法  ford() #in ...

随机推荐

  1. C语言 将产生的随机数存入数组,数据不能相同

    1.定义一个一维数,数组大小为24. 2.产生0~23的随机数. 3.将产生的随机数存入i数组,要求数组中的每个数据不能相同. 4.补充说明,这个子程序要求每次调用后,这个数组里面就 存放了0~23这 ...

  2. DB2物化表

    DB2物化查询表(MQT)是DB2数据库中一类特殊的表 物化表和视图的区别 物化表是一个查询结果集,视图是一个SQL语句. 以下是一个简单例子(说明物化表) 1.创建表,插入测试数据 ----创建表 ...

  3. java reflect 初始学习 动态加载类

    首先要理解Class类: 在java 的反射中,Class.forName("com.lilin.Office") 使用类的全名,这样获取,不仅仅表示了类的类类型,同时还代表着类的 ...

  4. Cygwin ssh服务配置 (SecureCRT连接Cygwin配置)

    1.运行ssh-host-config 这里需要注意的是标红部分,输入的用户名或密码要符合计算机的用户名或密码策略(尤其是公司有权限限制的电脑). $ ssh-host-config *** Quer ...

  5. Python常用内建模块

    Python常用内建模块 datetime 处理日期和时间的标准库. 注意到datetime是模块,datetime模块还包含一个datetime类,通过from datetime import da ...

  6. android开发系列之socket编程

    上周在项目遇到一个接口需求就是通讯系列必须是socket,所以在这篇博客里面我想谈谈自己在socket编程的时候遇到的一些问题. 其实在android里面实现一个socket通讯是非常简单的,我们只需 ...

  7. 历时八年,HTML5 标准终于完工了

    万维网联盟(W3C)2014年10月29日泪流满面地宣布,经过几乎8年的艰辛努力,HTML5标准规范终于最终制定完成了,并已公开发布. 在此之前的几年时间里,已经有很多开发者陆续使用了HTML5的部分 ...

  8. iOS-添加测试设备Identifier

    第一步:确认你的设备已经连接 第二步:点击xcode上"Windows"标签,选择"Devices" 第三步:在弹出的左框选择你要添加的设备.在右边框里可以找到 ...

  9. 使用JSON的数据格式

      在说JSON之前,我们先来看一下在javascript中创建对象的方式,也就是创建对象的字面量表示法.我们知道js中有五种基本的数据类型,分别是: Undefined(变量可能没有声明或者赋值) ...

  10. Ant学习---第二节:Ant添加文件夹和文件夹集的使用

    一.创建 java 项目(Eclipse 中),结构图如下: 1.创建 .java 文件,代码如下: package com.learn.ant; public class HelloWorld { ...