最大流—Ek算法(Edmond-Karp)

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. 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.

Input

Multiple test cases. For each case :

Line 1:     Two space-separated integers, N ( 0 ≤ N ≤ 100,000 ) 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, SiEi, and Ci.  Si and Ei ( 1 ≤ SiEi ≤ 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 one line with 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

--------------------------------------------------
这是一道典型的最大流题目,就是说要求从源点到经过的所有路径的最终到达汇点的所有流量和
源代码已ac
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<cstring>
 5 #define inf 4000005
 6 #define maxx 0x3f3f3f3f
 7 using namespace std;
 8 int n,m;
 9 int pre[inf],cap[inf][inf],flow[inf];
10 queue<int> q;
11 int bfs(int start,int en){
12     while(!q.empty())
13     q.pop();//继续清空之前的数据先
14     fill(pre,pre+inf,-1);
15      pre[start]=0;
16      flow[start]=maxx;
17      q.push(start);
18      while(!q.empty()){
19          int now=q.front();
20          q.pop();
21          if(now==en)
22          break;
23          for(int i=1;i<=m;i++){
24              if(i!=now&&cap[now][i]>0&&pre[i]==-1)
25              {
26                  pre[i]=now;//记录前驱
27                  flow[i]=min(cap[now][i],flow[now]);
28                  q.push(i);
29              }
30          }
31      }
32      if(pre[en]==-1)
33      return -1;//已经没法再狂增了
34      else
35      return flow[en];
36 }
37 int mostflow(int start,int en){
38     int k,add=0,sum=0,f,last;
39 
40     while((add=bfs(start,en))!=-1){
41 
42         f=en;
43         while(f!=start){
44             last=pre[f];//用前驱进行路径还原
45             cap[f][last]+=add;//改变正反向边的容量
46             cap[last][f]-=add;
47         f=last;
48         }
49         sum+=add;//加上最大流,后继续bfs找增加的
50     }
51     return sum;
52 }
53 int main(){
54 std::ios::sync_with_stdio(false);
55 while(cin>>n>>m){
56     fill(cap[0],cap[0]+205*205,0);
57       fill(flow,flow+inf,0);
58     for(int i=1;i<=n;i++){
59         int first,en,liu;
60         cin>>first>>en>>liu;
61         /*if(first==en)
62             continue;可要可不要*/
63         cap[first][en]+=liu;////以免有发生有多条边输入的情况
64     }
65     int ans=mostflow(1,m);///
66     cout<<ans<<endl;
67 }
68 
69 }

在此再贴一个极好的过程讲解方便学习https://blog.csdn.net/x_y_q_/article/details/51999466

猜你喜欢

转载自www.cnblogs.com/Yum20/p/9553793.html