poj 1273 -- Drainage Ditches

Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 55017 Accepted: 20992

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

最大流的模板题。因为没有clear容器。贡献了好多次wr。。。悲剧。

 1 /*======================================================================
 2  *           Author :   kevin
 3  *         Filename :   DrainageDitches.cpp
 4  *       Creat time :   2014-07-20 17:26
 5  *      Description :
 6 ========================================================================*/
 7 #include <iostream>
 8 #include <algorithm>
 9 #include <cstdio>
10 #include <cstring>
11 #include <queue>
12 #include <cmath>
13 #define clr(a,b) memset(a,b,sizeof(a))
14 #define M 205
15 #define INF 0x7f7f7f7f
16 using namespace std;
17 struct Edge{
18     int from,to,cap,flow;
19 };
20 int supers,supert,n,m;
21 vector<Edge>edges;
22 vector<int>G[M];
23 bool vis[M];
24 int d[M],cur[M];
25 
26 bool BFS()
27 {
28     clr(vis,0);
29     queue<int>Q;
30     Q.push(supers);
31     d[supers] = 0;
32     vis[supers] = 1;
33     while(!Q.empty()){
34         int x = Q.front(); Q.pop();
35         int len = G[x].size();
36         for(int i = 0; i < len; i++){
37             Edge& e = edges[G[x][i]];
38             if(!vis[e.to] && e.cap > e.flow){
39                 vis[e.to] = 1;
40                 d[e.to] = d[x]+1;
41                 Q.push(e.to);
42             }
43         }
44     }
45     return vis[supert];
46 }
47 int DFS(int x,int a)
48 {
49     if(x == supert || a == 0) return a;
50     int flow = 0,f;
51     int len = G[x].size();
52     for(int& i = cur[x]; i < len; i++){
53         Edge& e = edges[G[x][i]];
54         if(d[x] + 1 == d[e.to] && (f = DFS(e.to,min(a,e.cap-e.flow))) > 0){
55             e.flow += f;
56             edges[G[x][i]^1].flow -= f;
57             flow += f;
58             a -= f;
59             if(!a) break;
60         }
61     }
62     return flow;
63 }
64 void AddEdge(int from,int to,int cap)
65 {
66     edges.push_back((Edge){from,to,cap,0});
67     edges.push_back((Edge){to,from,0,0});
68     int m = edges.size();
69     G[from].push_back(m-2);
70     G[to].push_back(m-1);
71 }
72 int Dinic(int s,int t)
73 {
74     int flow = 0;
75     while(BFS()){
76         clr(cur,0);
77         flow += DFS(s,INF);
78     }
79     return flow;
80 }
81 int main(int argc,char *argv[])
82 {
83     while(scanf("%d%d",&n,&m)!=EOF){
84         edges.clear();
85         for(int i = 0; i < M; i++){
86             G[i].clear();
87         }
88         supers = 1;
89         supert = m;
90         int u,v,c;
91         for(int i = 0; i < n; i++){
92             scanf("%d%d%d",&u,&v,&c);
93             AddEdge(u,v,c);
94         }
95         int ans = Dinic(supers,supert);
96         printf("%d\n",ans);
97     }
98     return 0;
99 }
View Code

转载于:https://www.cnblogs.com/ubuntu-kevin/p/3857735.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值