POJ 1273 Drainage Ditches -dinic
来源:互联网 发布:visual studio mac版 编辑:程序博客网 时间:2024/05/22 04:48
dinic版本
感觉dinic算法好帅,比Edmonds-Karp算法不知高到哪里去了
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.
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 41 2 401 4 202 4 202 3 303 4 10
Sample Output
50
/*Dinic*/#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include<queue>using namespace std;const int inf=0x7fffffff;int n,m;//路径数 结点数int fl[200][200],dis[200];int bfs(){//memset(dis,-1,sizeof(dis));for(int i=1;i<=m+5;i++)dis[i]=-1;dis[1]=0;//原点层数为0queue<int> q;q.push(1);while(!q.empty()){int k=q.front();q.pop();for(int i=1;i<=m;i++){if(fl[k][i]>0 &&dis[i]<0){dis[i]=dis[k]+1;q.push(i);}}}//return (dis[m]>0);if(dis[m]>0)return 1;else return 0;}int dfs(int q,int mx){if(q==m)return mx;int i,a;for(i=1;i<=m;i++){if(fl[q][i]>0 && dis[i]==dis[q]+1 && (a=dfs(i,min(fl[q][i],mx))) ){fl[i][q]+=a;fl[q][i]-=a;return a;}}return 0;}int main(){int i,j,u,v,d;while(scanf("%d%d",&n,&m)!=EOF){memset(fl,0,sizeof(fl));for(i=1;i<=n;i++){ scanf("%d%d%d",&u,&v,&d);fl[u][v]+=d;}int ans=0;int res;while(bfs()){while(res=dfs(1,inf))ans+=res;}printf("%d\n",ans);}return 0;}
0 0
- POJ 1273 Drainage Ditches -dinic
- poj 1273 Drainage Ditches -- (Dinic 算法入门)
- poj 1273 Drainage Ditches--最大流--Dinic
- poj 1273 Drainage Ditches (最大流Dinic)
- POJ 1273 Drainage Ditches 最大流 dinic
- POJ 1273 Drainage Ditches (dinic模板)
- POJ 1273 Drainage Ditches(dinic模板)
- POJ-1273 Drainage Ditches 最大流Dinic
- 【POJ 1273】Drainage Ditches&Dinic详解
- poj 1273 Drainage Ditches【最大流 dinic】
- POJ 1273 Drainage Ditches 题解与分析<网络流DINIC>
- POJ 1273 && HDU 1532 Drainage Ditches (Dinic 算法)
- poj 1273 Drainage Ditches(最大流 dinic模版)
- 【最大流(dinic)】poj 1273 Drainage Ditches
- poj 1273--Drainage Ditches [网络流dinic算法]
- POJ 1273 Drainage Ditches (网络流 EK && Dinic)
- poj 1273Drainage Ditches(简单网络流(用dinic))
- POJ 1273~Drainage Ditches(网络最大流ek,dinic)
- 网络流二十四题之九 —— 方格取数问题(GRID)
- 适配器的优化
- java常用工具类之DES和Base64加密解密类
- activity 背景色 透明
- java多线程系列四:synchronized
- POJ 1273 Drainage Ditches -dinic
- bzoj3158 千钧一发
- ACM 搜索 hdu1010 Tempter of the Bone
- 1670 打怪兽
- Web
- Java设计模式之代理设计模式
- 产品经理常用设计方法之一
- 24-网络编程http和简单的JSON
- hdu 2059 龟兔赛跑 DP