PKU1273Drainage Ditches
2015-12-29 19:28
447 查看
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。。
邻接矩阵版:
next数组版:(感谢Claris学长。。)
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 >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 >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; }
相关文章推荐
- 思杰成为ADC领域在Container容器中提供其产品的第一家主要厂商
- 浅谈RAID写惩罚(Write Penalty)与IOPS计算
- 关于Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.的问题
- 后台数据校验
- assign与retain
- @property (nonatomic, retain)
- time_wait详解
- RAID的各级特性及软RAID的创建
- 怎么解决啊???"Thread-14" HibernateException: Could not obtain transaction-.....
- ibm aix I/O ERROR DETECTED BY LVM
- 自定义View时,用到Paint Canvas的一些温故,讲讲平时一些效果是怎么画的(基础篇 二,图像遮盖,Canvas静态变化)
- 自定义View时,用到Paint Canvas的一些温故,讲讲平时一些效果是怎么画的(基础篇 二,图像遮盖,Canvas静态变化)
- Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
- EC纠删码(Erasure Code)校验码算法相比传统RAID5有哪些优势?
- 关于工作,学习中定时备份的几个方法(cron,git,mail)
- 打造终端下mutt收发邮件环境(fbterm,fetchmail,msmtp,procmail,mutt)
- 3D空间中的AABB(轴向平行包围盒, Aixe align bounding box)的求法
- Custom Container View Controller容器vc
- http://blog.csdn.net/techbirds_bao/article/details/9233599
- 最新Google Gmail邮箱登陆不了解决方法(hosts更新于12月14日)