1. 程式人生 > >POJ-1273 Drainage Ditches (網絡流入門題)

POJ-1273 Drainage Ditches (網絡流入門題)

queue data integer its determine one ase printf ons

Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 78642 Accepted: 30681

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 填補了laj在網絡流上的空白qwq Dinic算法詳解見:http://www.cnblogs.com/SYCstudio/p/7260613.html Dinic算法最大流模板見:http://www.cnblogs.com/keximeiruguo/p/7749626.html
 1 #include "bits/stdc++.h"
 2 using namespace std;
 3 typedef long long LL;
 4 const int MAX=205;
 5 int n,m,s,t;
 6 int tot,head[MAX],adj[MAX<<1],wei[MAX<<1],next[MAX<<1];
 7 int cur[MAX],deep[MAX];
 8 void addedge(int u,int v,int w){
 9     tot++;adj[tot]=v,wei[tot]=w,next[tot]=head[u],head[u]=tot;
10 }
11 bool bfs(){
12     int i,j;
13     memset(deep,127,sizeof(deep));
14     deep[s]=0;
15     queue <int> q; q.push(s);
16     while (!q.empty()){
17         int u=q.front();q.pop();
18         for (i=head[u];i;i=next[i]){
19             if (deep[adj[i]]>1e9 && wei[i]>0){
20                 deep[adj[i]]=deep[u]+1;
21                 q.push(adj[i]);
22             }
23         }
24     }
25     return deep[t]<1e9;
26 }
27 int dfs(int x,int flo){
28     if (x==t || flo==0) return flo;
29     int j;
30     for (int &i=cur[x];i;i=next[i]){
31         if (deep[adj[i]]==deep[x]+1 && wei[i]>0){
32             j=dfs(adj[i],min(flo,wei[i]));
33             if (j){
34                 wei[i]-=j;
35                 wei[i^1]+=j;
36                 return j;
37             }
38         }
39     }
40     return 0;
41 }
42 int main(){
43     freopen ("ditches.in","r",stdin);freopen ("ditches.out","w",stdout);
44     int i,j,u,v,w;
45     while (~scanf("%d%d",&m,&n)){
46         tot=1;s=1,t=n;
47         memset(head,0,sizeof(head));
48         for (i=1;i<=m;i++){
49             scanf("%d%d%d",&u,&v,&w);
50             addedge(u,v,w),addedge(v,u,0);
51         }
52         int flow=0,dd;
53         while (bfs()){
54             for (i=1;i<=n;i++) cur[i]=head[i];
55             while (dd=dfs(s,1e9)) flow+=dd;
56         }
57         printf("%d\n",flow);
58     }
59     return 0;
60 }

POJ-1273 Drainage Ditches (網絡流入門題)