HDU 1523 Drainage Ditches (网络流/dinic/模板)
来源:互联网 发布:安卓开发java基础 编辑:程序博客网 时间:2024/06/03 23:24
#include<queue>#include<stdio.h>#include<string.h>using namespace std;const int maxn=1e6;struct Edge{ int a,b,c; int next;}edge[maxn];int head[maxn];int n,m,cnt;int step[maxn];int sp,ep;void Addedge(int a,int b,int c){ edge[cnt].a=a; edge[cnt].b=b; edge[cnt].c=c; edge[cnt].next=head[a]; head[a]=cnt++;}int bfs(){ queue<int>Q; memset(step,-1,sizeof(step)); step[sp]=0; Q.push(sp); while(!Q.empty()) { int cur=Q.front(); Q.pop(); for(int i=head[cur];i!=-1;i=edge[i].next) { int u=edge[i].b; if(step[u]==-1&&edge[i].c>0) { step[u]=step[cur]+1; Q.push(u); } } } return step[ep]!=-1;}int dfs(int x,int y){ int r; if(x==ep) return y; for(int i=head[x];i!=-1;i=edge[i].next) { int cur=edge[i].b; if(edge[i].c>0&&step[cur]==step[x]+1) { r=dfs(cur,min(y,edge[i].c)); if(r>0) { edge[i].c=edge[i].c-r; edge[i^1].c=edge[i^1].c+r; return r; } } } return 0;}int dinic(int sp,int ep){ int ans=0,t; while(bfs()) { while(t=dfs(sp,maxn)) { ans=ans+t; } } return ans;}int main(){ while(~scanf("%d%d",&n,&m)) { cnt=0; memset(head,-1,sizeof(head)); for(int i=0;i<n;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); Addedge(a,b,c); Addedge(b,a,0); } sp=1; ep=m; printf("%d\n",dinic(sp,ep)); }}
Drainage Ditches
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15482 Accepted Submission(s): 7378
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.
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
Source
USACO 93
Recommend
lwg | We have carefully selected several similar problems for you: 1533 3338 1569 3572 3416
0 0
- HDU 1523 Drainage Ditches (网络流/dinic/模板)
- hdu 1532 Drainage Ditches(网络流dinic模板)
- HDU 1532 Drainage Ditches 网络流模板题(Dinic)
- hdu 1532 Drainage Ditches 网络最大流模板 EK + Dinic两种算法AC
- usaco Drainage Ditches(网络流dinic模板)
- HDU 1532 Drainage Ditches 网络流模板
- HDU1532 Drainage Ditches 最大流Dinic模板
- [hdu 1532] Drainage Ditches(最大流dinic)
- hdu Drainage Ditches(网络流)
- POJ--1273--Drainage Ditches【Dinic模板】【isap模板】网络最大流
- HDU 1532 Drainage Ditches(Dinic)
- HDU 1532 Drainage Ditches 网络流 模板题
- 网络流模板-(EdmondsKarp)-HDU-1532-Drainage Ditches
- POJ 1273 Drainage Ditches—网络流,最大流模板题,Dinic算法
- poj 1273 & hdu 1532 Drainage Ditches(最大流 )EK,dinic模板
- hdu 1532 Drainage Ditches(最大流 三种模板:EK、Dinic、isap)
- POJ1273 Drainage Ditches 最大流模板题(dinic)
- POJ 1273 --Drainage Ditches【最大流模板 && dinic】
- CodeForces158B
- 二叉树学习总结
- Android异步下载网络图片(其二:AsyncTask)
- shape里面的gradient属性,就是渐变属性
- hdu5556 Land of Farms
- HDU 1523 Drainage Ditches (网络流/dinic/模板)
- JQuery之子复选框全部选中则全选框选中
- Android异步下载网络图片(其三:ExecutorService)
- Android开发之局域网UDP传输
- 使用Dom4j解析XML
- jquery 随鼠标移动的导航条
- 206. Reverse Linked List
- python3中builtwith包不能用怎么办
- 安卓UI学习(1)--绘制带阴影的按钮