PKU1273Drainage Ditches
来源:互联网 发布:阴阳师攻击力数据排行 编辑:程序博客网 时间:2024/06/11 05:23
Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 65138 Accepted: 25108
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
USACO 93
网络流裸题,Dinic算法。。
在这里感谢DaD3zZ。。
邻接矩阵版:
#include<cstdio>#include<cstring>#include<iostream>using namespace std;int f[201][201],head,tail,dis[201],ans=0,sum=0,n,m,i,x,y,w,q[201];int bfs(){ int j,p; memset(dis,0xff,sizeof(dis)); dis[1]=0; head=0; tail=1; q[1]=1; while (head<tail) { head++; p=q[head]; for (j=1;j<=n;j++) if (dis[j]<0 && f[p][j]>0) { dis[j]=dis[p]+1; tail++; q[tail]=j; } } if (dis[n]>0) return 1; else return 0;}int dfs(int s,int low){ int j,a=0; if (s==n) return low; for (j=1;j<=n;j++) if (f[s][j]>0 && dis[j]==dis[s]+1 && (a=dfs(j,min(low,f[s][j])))) { f[s][j]-=a; f[j][s]+=a; return a; } return 0;}int main(){ while (scanf("%d %d",&m,&n)!=EOF) { memset(f,0,sizeof(f)); for (i=1;i<=m;i++) { scanf("%d %d %d",&x,&y,&w); f[x][y]+=w; } ans=0; while (bfs()) while (sum=dfs(1,0x7fffffff)) ans+=sum; printf("%d\n",ans); } return 0;}
next数组版:(感谢Claris学长。。)
#include<cstdio>#include<cstring>#include<iostream>using namespace std;struct node{ int next,to,v;};node edge[500001]={0};int cnt=1,h[500001]={0},q[500001],head,tail,dis[500001],n,m,ans,sum=0;int read(){ int x=0,f=1; char ch=getchar(); while (ch<'0' || ch>'9') { if (ch=='-') f=-1; ch=getchar(); } while (ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f;}void add(int u,int v,int w){ cnt++; edge[cnt].next=h[u]; h[u]=cnt; edge[cnt].to=v; edge[cnt].v=w;}bool bfs(){ int j,p; memset(dis,-1,sizeof(dis)); q[1]=1; dis[1]=0; head=0; tail=1; while (head<tail) { head++; j=q[head]; p=h[j]; while (p) { if (dis[edge[p].to]<0 && edge[p].v>0) { dis[edge[p].to]=dis[j]+1; tail++; q[tail]=edge[p].to; } p=edge[p].next; } } if (dis[n]>0) return true; else return false;}int dfs(int s,int low){ int j,a=0; if (s==n) return low; j=h[s]; while (j) { if (edge[j].v>0 && dis[edge[j].to]==dis[s]+1 && (a=dfs(edge[j].to,min(low,edge[j].v)))) { edge[j].v-=a; edge[j^1].v+=a; return a; } j=edge[j].next; } return 0;}int main(){ int i,s,e,v; while (scanf("%d %d",&m,&n)!=EOF) { memset(edge,0,sizeof(edge)); for (i=1;i<=m;i++) { s=read(); e=read(); v=read(); add(s,e,v); add(e,s,0); } ans=0; while (bfs()) while (sum=dfs(1,0x7fffffff)) ans+=sum; printf("%d\n",ans); } return 0;}
- PKU1273Drainage Ditches
- poj1273Drainage Ditches
- poj_1273Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- poj_1273_Drainage Ditches
- hdu_1532_Drainage Ditches
- 12.28总结
- 碎片化管理
- 查看自己电脑可以支持的最大内存量
- swift ios 平移动画(8种方式)
- ModelSim 与Debussy联调
- PKU1273Drainage Ditches
- 标志寄存器 x86汇编
- ZooKeeper单机模式(stand-alone)
- 关于html页面布局
- UVA10344 23 out 5
- 宏定义
- <<Hive编程指南>>读书笔记
- 【SQL Server】SQL2005的安装与配置、简单使用
- JAVA ServiceLoader详解