poj1273 Drainage Ditches
来源:互联网 发布:131458淘宝卖家工具箱 编辑:程序博客网 时间:2024/05/17 09:14
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.
最大流模板题。dinic算法。
#include<cstdio>#include<cstring>#define M(a) memset(a,0,sizeof(a))int min(int x,int y){return x<y?x:y;}const int inf=0x7f7f7f7f;int n,m,fir[201],f[201],to[403],cap[403],next[403];bool make(){int q[201]={0},hd,tl,i,x,y,z;hd=tl=q[1]=1;memset(f,0xff,sizeof(f));f[1]=0;while (hd<=tl){x=q[hd++];for (i=fir[x];i;i=next[i]) if (f[to[i]]==-1&&cap[i]) { q[++tl]=to[i]; f[to[i]]=f[x]+1; }}return f[m]!=-1;}int spr(int p,int max){if (p==m) return max;int i,j,x,y,z,ans=0;for (i=fir[p];i;i=next[i]){if (f[to[i]]==f[p]+1&&cap[i]){x=spr(to[i],min(cap[i],max-ans));cap[i]-=x;ans+=x;cap[i^1]+=x;}}if (ans==0) f[p]=-1;return ans;}int main(){int i,j,k,x,y,z,ans;while (scanf("%d%d",&n,&m)==2){M(fir);M(next);M(f);M(to);M(cap);for (i=1;i<=n;i++){scanf("%d%d%d",&x,&y,&z);to[i*2]=y;cap[i*2]=z;next[i*2]=fir[x];fir[x]=i*2;to[i*2+1]=x;cap[i*2+1]=0;next[i*2+1]=fir[y];fir[y]=i*2+1;}ans=0;while (make()) while (x=spr(1,inf)) ans+=x;printf("%d\n",ans);}}
0 0
- POJ1273 Drainage Ditches
- poj1273 Drainage Ditches
- poj1273 Drainage Ditches
- POJ1273:Drainage Ditches
- poj1273 - Drainage Ditches
- POJ1273(Drainage Ditches)
- POJ1273-Drainage Ditches
- poj1273 Drainage Ditches
- POJ1273 Drainage Ditches
- poj1273 Drainage Ditches
- POJ1273 Drainage Ditches
- poj1273 Drainage Ditches
- poj1273 Drainage Ditches
- POJ1273 Drainage Ditches
- POJ1273&&HDU1532-Drainage Ditches
- POJ1273 Drainage Ditches(dinic)
- POJ1273-Drainage Ditches
- POJ1273 Drainage Ditches (SAP+GAP)
- 当前时间显示
- android测试框架对比(一)
- 浙大 PAT Advanced level 1015. Reversible Primes (20)
- 小型数据的缓存
- eclipse中修改启动tomcat时的jvm参数
- poj1273 Drainage Ditches
- android BroadCast的发送与接收
- Android中intent传递list或者对象的方法
- NPD实现及其与pico一脉相承的关系
- poj 2318(计算几何)
- nodejs express req.body 为undefined的一个bug
- Intent和IntentFilter详解
- 浙大 PAT Advanced level 1016. Phone Bills (25)
- consumer抓取消息--一个线程抓取,N个consumer的n个线程消费