poj1273 Drainage Ditches(裸最大流)

 

                                                        Drainage Ditches

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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

思路:单源单汇最大流问题,这次用到了残余网络,每次找到一条增广路,min里存的是所有相连边的最小值,不是这条增广路上的最小值,然后此条增广路

上的每条边减去最小值,再重新开始找增广路,重复执行以上操作,直到没有增广路为止。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 4 using namespace std;
 5 
 6 int cap[205][205],pre[205];//cap 残量(残余网络),n 汇点
 7 int n;
 8 queue<int>p;
 9 
10 int _min(int a,int b)
11 {
12     return a<b?a:b;
13 }
14 
15 void bfs()
16 {
17     int Maxflow = 0, i;
18     while(1)
19     {
20         pre[1]=1;
21         p.push(1);
22         int min=999999999;
23         memset(pre,0,sizeof(pre));
24         while(!p.empty())
25         {
26             int u=p.front();
27             p.pop();
28             for(i=1;i<=n;i++)
29             {
30                 if(pre[i]==0  && cap[u][i]>0)
31                 {
32                     pre[i]=u;
33                     p.push(i);
34                     min=_min(min,cap[u][i]);//找到此时遍历到的所有边的最短值,并不是增广路上的最小容量值
35                 }
36             }
37         }
38         if(pre[n]==0)  break;
39 
40         for(int s=n; s!=1; s=pre[s]){
41             cap[pre[s]][s]-= min;
42             cap[s][pre[s]]+= min;
43         }
44         Maxflow += min;
45     }
46     printf("%d ", Maxflow);
47 }
48 
49 int main()
50 {
51     int m, a, b, c, i;
52     while(scanf("%d%d",&m,&n)!=EOF)
53     {
54         memset(cap,0,sizeof(cap));
55         for(i=0;i<m;i++)
56         {
57             scanf("%d%d%d",&a,&b,&c);
58             cap[a][b]+=c;
59         }
60         bfs();
61     }
62     return 0;
63 }
View Code

每天训练发现我比别人做的好慢,但是理解的更深刻,如果一开始学一个新知识点就搜模板,那么这样的人是走不远的,毕业之后带走的只有思维,什么荣誉,奖杯都已经不重要了。
原文地址:https://www.cnblogs.com/6bing/p/3928340.html