poj1273 Drainage Ditches 最大流 dinic算法
来源:互联网 发布:银行大数据是什么意思 编辑:程序博客网 时间:2024/05/16 08:24
Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 49853 Accepted: 18918
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
灰常经典的最大流,我是用的dinic算法,我们可以看出这个算法,是很快的,先用bfs,再用dfs,这样就非常好的效果!
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;#define typec intconst typec inf=0x3f3f3f3f;#define E 402#define N 200struct edge{int x,y,nxt;typec c;}bf[E];int ne,head[N],cur[N],ps[N],dp[N],dep[N];void addedge(int x,int y,typec c){ bf[ne].x=x;bf[ne].y=y;bf[ne].c=c; bf[ne].nxt=head[x];head[x]=ne++; bf[ne].x=y;bf[ne].y=x;bf[ne].c=0; bf[ne].nxt=head[y];head[y]=ne++;}typec flow(int n,int s,int t){ typec tr,res=0; int i,j,k,f,r,top; while(1) { memset(dep,-1,n*sizeof(int)); for(f=dep[ps[0]=s]=0,r=1;f!=r;) { for(i=ps[f++],j=head[i];j;j=bf[j].nxt) { if(bf[j].c&&-1==dep[k=bf[j].y]) { dep[k]=dep[i]+1;ps[r++]=k; if(k==t) { f=r;break; } } } } if(-1==dep[t])break; memcpy(cur,head,n*sizeof(int)); for(i=s,top=0;;) { if(i==t) { for(k=0,tr=inf;k<top;++k) { if(bf[ps[k]].c<tr) tr=bf[ps[f=k]].c; } for(k=0;k<top;k++) { bf[ps[k]].c-=tr,bf[ps[k]^1].c+=tr; } res+=tr;i=bf[ps[top=f]].x; } for(j=cur[i];cur[i];j=cur[i]=bf[cur[i]].nxt) if(bf[j].c&&dep[i]+1==dep[bf[j].y])break; if(cur[i]) { ps[top++]=cur[i]; i=bf[cur[i]].y; } else { if(0==top)break; dep[i]=-1;i=bf[ps[--top]].x; } } } return res;}int main(){ int n,m,i,s,e,val; while(scanf("%d%d",&n,&m)!=EOF) { memset(head,0,sizeof(head)); ne=2; for(i=0;i<n;i++) { scanf("%d%d%d",&s,&e,&val); s--,e--; addedge(s,e,val); } printf("%d\n",flow(m,0,m-1)); } return 0;}
- poj1273 Drainage Ditches 最大流 dinic算法
- POJ1273 Drainage Ditches 【最大流Dinic】
- poj1273 Drainage Ditches(最大流dinic板子)
- POJ1273 Drainage Ditches【dinic算法】
- POJ1273 Drainage Ditches(裸最大流,EK,DINIC)
- POJ1273 Drainage Ditches 最大流模板题(dinic)
- poj1273 Drainage Ditches(最大流EKarp+Dinic+模板总结)
- POJ1273 Drainage Ditches(dinic)
- POJ1273 Drainage Ditches(最大流)
- POJ1273 Drainage Ditches 【最大流】
- 【poj1273】Drainage Ditches(Dinic)
- POJ1273 Drainage Ditches【最大流、增广路算法Edmonds_Karp】
- poj1273 Drainage Ditches(最大流入门 EK+Dinic模板)
- POJ1273 Drainage Ditches——最大流
- 网络最大流问题 poj1273 Drainage Ditches
- POJ1273 Drainage Ditches最大流模板
- poj1273 Drainage Ditches 最大流EK
- poj1273--Drainage Ditches(最大流)
- iOS 开发之设置UIButton(温故知新)
- JAVA新人常犯错误集锦
- NandFlash系列之二:S3C2410读写Nand Flash分析 .
- Why does DBA_TAB_MODIFICATIONS sometimes have no values [ID 762738.1]
- linux 实时时钟(RTC)驱动 .
- poj1273 Drainage Ditches 最大流 dinic算法
- 有无序的实数列 V[N],要求求里面大小相邻的实数的差的最大值,关键是要求线性空 间和线性时间
- Android照片墙分享.听说很美观
- 工作累了,博大家一乐
- V4L2驱动程序架构
- WindowProc的使用
- java泛型和反射的问题
- 终结:容量为a、b的两个杯子倒出容量c的水
- 常用PHP正则表达式