三种模板:Edmonds_Karp,Dinic,SAP

例题:

Drainage Ditches(HDU1532)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22365    Accepted Submission(s): 10683)

Problem 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

5 4

1 2 40

1 4 20

2 4 20

2 3 30

3 4 10

Sample Output

50

Source

HDU1532

题意:最大流模板题

方法一:Edmonds_Karp

#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
const int maxn=205;
const int INF=0x3f3f3f3f;
using namespace std;struct Edge
{int from,to,cap,flow;Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};vector<Edge> edges;
vector<int> G[maxn];
int a[maxn];
int pre[maxn];void init(int n)
{for (int i=0;i<n;i++)G[i].clear();edges.clear();
}void addedge(int from,int to,int cap)
{edges.push_back(Edge(from,to,cap,0));edges.push_back(Edge(to,from,0,0));int m=edges.size();G[from].push_back(m-2);G[to].push_back(m-1);
}int Edmonds_Karp(int s,int t)
{int flow=0;while(1){memset(a,0,sizeof(a));queue<int> Q;Q.push(s);a[s]=INF;while(!Q.empty()){int x=Q.front();Q.pop();int sz=G[x].size();for (int i=0;i<sz;i++){Edge& e=edges[G[x][i]];if (!a[e.to]&&e.cap>e.flow){pre[e.to]=G[x][i];a[e.to]=min(a[x],e.cap-e.flow);Q.push(e.to);}}if (a[t])break;}if (!a[t])break;for (int u=t;u!=s;u=edges[pre[u]].from){edges[pre[u]].flow+=a[t];edges[pre[u]^1].flow-=a[t];}flow+=a[t];}return flow;
}int main()
{int n,m,u,v,f;while(cin >> n >> m){init(n);for (int i=0;i<n;i++){scanf("%d%d%d",&u,&v,&f);addedge(u,v,f);}cout << Edmonds_Karp(1,m) << endl;}return 0;
}

方法二:Dinic

#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
const int maxn=205;
const int INF=0x3f3f3f3f;
using namespace std;struct Edge
{int from,to,flow;Edge(int u,int v,int f):from(u),to(v),flow(f){}
};vector<Edge> edges;
vector<int> G[maxn];int dis[maxn];
int cur[maxn];void init(int n)
{for (int i=0;i<n;i++)G[i].clear();edges.clear();
}void addedge(int from,int to,int flow)
{edges.push_back(Edge(from,to,flow));edges.push_back(Edge(to,from,0));int m=edges.size();G[from].push_back(m-2);G[to].push_back(m-1);
}bool bfs(int s,int t)
{queue<int> Q;Q.push(s);memset(dis,-1,sizeof(dis));dis[s]=0;while(!Q.empty()){int x=Q.front();Q.pop();int sz=G[x].size();for (int i=0;i<sz;i++) {Edge& e=edges[G[x][i]];if (e.flow>0) {if (dis[e.to]<0) {dis[e.to]=dis[x]+1;Q.push(e.to);}}}}return bool(~dis[t]);
}int dfs(int s,int t,int maxflow)
{if (s==t)return maxflow;int sz=int(G[s].size());for (int i=cur[s],num;num=G[s][i],i<sz;i++){cur[s]=i;Edge &e=edges[num];if (dis[e.to]==dis[s]+1 && e.flow>0){int flow=dfs(e.to,t,min(maxflow,e.flow));if (flow!=0){e.flow-=flow;edges[num^1].flow+=flow;return flow;}}}return 0;
}int Dinic(int s,int t)
{int ans=0;while(bfs(s,t)){int flow;memset(cur,0,sizeof(cur));while((bool)(flow=dfs(s,t,INF)))ans+=flow;}return ans;
}int main()
{int n,m,u,v,f;while(cin >> n >> m){init(n);for (int i=0;i<n;i++){scanf("%d%d%d",&u,&v,&f);addedge(u,v,f);}cout << Dinic(1,m) << endl;}return 0;
}

方法三:SAP

#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
const int maxn=205;
const int maxm=maxn*maxn;
const int INF=0x3f3f3f3f;
using namespace std;struct Edge
{  int v,w,next;
}edge[maxm];int dis[maxn],pre[maxn],rec[maxn],head[maxn],gap[maxn],now[maxn];
int n,m,no,up;
queue<int> q;void addedge(int u,int v,int w)
{edge[no].v=v; edge[no].w=w;edge[no].next=head[u]; head[u]=no++;edge[no].v=u; edge[no].w=0;edge[no].next=head[v]; head[v]=no++;
}void pre_init()
{  no=0; up=n;memset(head,-1,sizeof(head));
}void init(int s,int t)
{for(int i=0;i<=up;i++) {now[i]=head[i];gap[i]=0;dis[i]=INF;}while(!q.empty())q.pop();dis[t]=0; q.push(t);while(!q.empty()){int tp=q.front();q.pop();gap[dis[tp]]++;int k=head[tp];while(k!=-1){if(dis[edge[k].v]==INF && edge[k^1].w)  {dis[edge[k].v]=dis[tp]+1;  q.push(edge[k].v);  }k=edge[k].next;  }}
}int SAP(int s,int t)
{  int ans=0,flow=INF,top=s;  pre[s]=s;init(s,t);  while(dis[s]<up)   {  if(top==t)  {  ans+=flow;  while(top!=s){  edge[rec[top]].w-=flow;  edge[rec[top]^1].w+=flow;  top=pre[top];  }flow=INF;  }  int k=now[top];  while(k!=-1)  {  int v=edge[k].v;  if(edge[k].w && dis[top]==dis[v]+1)  {  flow=min(flow, edge[k].w);  pre[v]=top; rec[v]=k;  now[top]=k;   top=v;  break;  }  k=edge[k].next;  }  if(k==-1){  int mind=n;  if(--gap[dis[top]]==0) break;int k=now[top]=head[top];while(k!=-1)  {  if(edge[k].w && mind>dis[edge[k].v])mind=dis[edge[k].v];  k=edge[k].next;  }  gap[dis[top]=mind+1]++;  top=pre[top];  }}return ans;
}int main()
{int u,v,f;while(cin >> n >> m){pre_init();for (int i=0;i<n;i++){scanf("%d%d%d",&u,&v,&f);addedge(u,v,f);}cout << SAP(1,m) << endl;}return 0;
}

转载于:https://www.cnblogs.com/Radium1209/p/10415348.html

最大流自用模板(例题:HDU1532)相关推荐

  1. 树链剖分概念及模板 + 例题 [POJ3237 tree + 软件包管理器]

    文章目录 概念 模板 例题1:软件包管理器 题目 题解 代码实现 例题2:POJ3237 tree 题目 题解 代码实现 概念 树链剖分主要是用于解决以下这两个问题. 1.更改树上点x到点y的最短路径 ...

  2. 学习KMP (概念 + 模板 + 例题: 子串查找)

    我又回来了,感jio这几天有点勤啊!! 这一次我带着KMP来了, 文章目录 KMP介绍 模板 例题: 子串查找 题目 暴力题解 KMP题解 代码实现 KMP介绍 KMP,即 Knuth-Morris- ...

  3. 学习三分 (概念 + 模板 + 例题:曲线)

    这好像是我第一次尝试写一个新知识入门 而不是习题解 文章目录 三分概念 模板 例题:曲线 题目 题解 代码实现 三分概念 我们都知道,二分是在一个单调函数(即一次函数)上通过每次查找折半的方式,对答案 ...

  4. 最大流算法模板:EK和dinic算法

    最大流算法模板:EK和dinic算法 一.EK算法模板 #include<iostream> #include<queue> using namespace std; cons ...

  5. [学习笔记] 伸展树splay详解+全套模板+例题[Luogu P3369 【模板】普通平衡树]

    文章目录 引入概念 全套模板 变量声明 update ==rotate旋转== splay操作 insert插入 delete删除 查找x的位置 查找第k大 前驱/后继 极小值-inf和极大值inf的 ...

  6. 后缀自动机(模板+例题)

    存个模板 void get_SAM(int w) {//加入的字母-'a' //c[w]域是一个DAG 存的是转移边 也就是后缀自动机 而par域是存辅助建后缀自动机的parent树 int x=++ ...

  7. 最新帝国CMS花生小说系统源码+花生日记引流导航模板+带采集工具

    正文: 一个专注小说阅读细化分类的网站. 说明:小说公众号导航站模板,小说网站导航引流网站源码,网站结构清晰优化得好容易收录. 本站带火车自动采集,带手机端,并且有同步生成插件,采用帝国cms7.5内 ...

  8. 最大流、最小费用最大流【模板】

    一下代码版权归:HIT    xiaodai 最大流模板:(题目链接) #include <cstring> #include <algorithm> #include < ...

  9. 最小费用最大流 【模板】

    如果理解了最大流连续增广路算法的思维, 理解这个算法还是很简单的. 结构体存储信息: 分别为边的起点.终点.容量.当前流量.费用.下一条边的编号. struct Edge {int from, to, ...

最新文章

  1. 推荐Scum敏捷开发的几款工具
  2. 【Network Security!】信息的扫描与嗅探
  3. 服务器不重启磁盘修复,重启后数据盘不见了?别担心,只是磁盘脱机
  4. arcgis 获取json经纬度_干货|ArcGIS的矢量化操作——ArcGis中进行地形图的配准
  5. WEBSHELL权限提升 菜菜
  6. 这所高校“起高楼”,绊倒两任“一把手”
  7. java类无法调用值,Kotlin无法调用到Java中定义的interface类的问题记录
  8. iOS 修改webView字体
  9. Spring MVC笔记 使用JdbcTemplate
  10. C语言程序设计谭浩强版 五
  11. idea调试jdk源码
  12. PPT一般使用技巧总结
  13. oracle北京时区,ORACLE中的时区(time zone)
  14. 如何获取excel 中的 某几个列的值
  15. 科研绘图(Matplotlib.pyplot)
  16. to_char在oracle 中函数使用方法
  17. 在线aoi测试软件打不开,在线aoi光学检测仪
  18. www.ty66.php,韬轩阁
  19. CSS中继承性属性和非继承性的属性
  20. 中醫秘笈大公開---家家必備十良方

热门文章

  1. SQL语句增加字段、修改字段、修改类型、修改默认值
  2. Linux中设置vim自动在运算符号两边加上空格
  3. C#中ComboBox动态绑定赋值
  4. underscorejs-groupBy学习
  5. Client does not support authentication protocol requested by server;
  6. N个三角形分割平面个数(数学)
  7. 星期三,今天早上上了四节JS课程,下午听健康讲座,晚上装系统
  8. 有赞美业微前端的落地总结
  9. Mysql 开启远程连接
  10. javascript --- [虚拟DOM] 初始化 实现