USACO 4.2 Drainage Ditches(网络流模板题)

Drainage Ditches
Hal Burch

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. Note however, that there can be more than one ditch between two intersections.

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.

PROGRAM NAME: ditch

INPUT FORMAT

Line 1: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.
Line 2..N+1:Each of 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.

SAMPLE INPUT (file ditch.in)

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

OUTPUT FORMAT

One line with a single integer, the maximum rate at which water may emptied from the pond.

SAMPLE OUTPUT (file ditch.out)

50
————————————————————————————————————————————————
它有个俗名叫草地排水
 1 /*
 2 ID:ivorysi
 3 PROG:ditch
 4 LANG:C++
 5 */
 6 #include <iostream>
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <queue>
10 #include <set>
11 #include <vector>
12 #define inf 0x7fffffff
13 #define ivorysi
14 #define siji(i,x,y) for(int i=x;i<=y;++i)
15 #define gongzi(j,x,y) for(int j=x;j>=y;--j)
16 #define xiaosiji(i,x,y) for(int i=x;i<y;++i)
17 #define sigongzi(j,x,y) for(int j=x;j>y;--j)
18 using namespace std;
19 struct node {
20     int to,next,val;
21 }edge[505];
22 int head[305],sum=1;
23 void add(int u,int v,int c) {
24     edge[++sum].to=v;
25     edge[sum].next=head[u];
26     edge[sum].val=c;
27     head[u]=sum;
28 }
29 void AddTwoWays(int u,int v,int c) {
30     add(u,v,c);
31     add(v,u,0);//反向边
32 }
33 int n,m,dis[305],gap[305],ans;
34 int sap(int u,int aug){//O(玄学)
35     if(u==m) return aug;
36     int dmin=m-1,flow=0,v,t;
37 
38     for(int i=head[u];i;i=edge[i].next) {
39         v=edge[i].to;
40         if(edge[i].val>0) {//只有还能流动的地方才能分层
41             if(dis[u]==dis[v]+1) {
42                 t=sap(v,min(aug-flow,edge[i].val));//限制流量不超过该点流量和边的限制
43                 edge[i].val-=t;
44                 edge[i^1].val+=t;
45                 flow+=t;
46                 if(aug==flow) return flow;//流尽
47                 if(dis[1]>=m) return flow;//搜索已在某处达到结束条件
48             }
49             dmin=min(dmin,dis[v]);
50         }
51     }
52     if(!flow) {
53         --gap[dis[u]];
54         if(gap[dis[u]]==0) dis[1]=m;//出现断层说明无法再流
55         dis[u]=dmin+1;//分层
56         ++gap[dis[u]];
57     }
58     return flow;
59 }
60 int main(int argc, char const *argv[])
61 {
62 #ifdef ivorysi
63     freopen("ditch.in","r",stdin);
64     freopen("ditch.out","w",stdout);
65 #else
66     freopen("f1.in","r",stdin);
67 #endif    
68     scanf("%d%d",&n,&m);
69     int u,v,c;
70     siji(i,1,n) {
71         scanf("%d%d%d",&u,&v,&c);
72         AddTwoWays(u,v,c);
73     }
74     while(dis[1]<m) {ans+=sap(1,inf);}
75     printf("%d\n",ans);
76     return 0;
77 }

 

 

转载于:https://www.cnblogs.com/ivorysi/p/6290984.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值