最大流的一个代码实现

转载自http://www.cppblog.com/mythit/archive/2009/04/19/80470.aspx 增加了一点点注释~~

终于看懂了,好开心~~~

希望现在学习,还能赶上别人!

应用实例

  这是一道最大流的入门题,题目如下:

  http://acm.pku.edu.cn/JudgeOnline/problem?id=1273

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

 1 // zuidaliu.cpp : Defines the entry point for the console application.
 2 //
 3 
 4 #include "stdafx.h"
 5 
 6 
 7 #include <iostream>
 8  #include <queue>
 9  using namespace std;
10 
11  const int N = 210;
12  const int INF = 0x7FFFFFFF;//起点的流量是无穷大
13  int n, m, map[N][N], path[N], flow[N], start, i_end;
14  queue<int> q;
15 
16  int bfs(){
17      /*
18      设队列Q:存储当前未访问的节点,队首节点出队后,成为已检查的标点;
19 
20      Path数组:存储当前已访问过的节点的增广路径;
21 
22      Flow数组:存储一次BFS遍历之后流的可改进量;
23      */
24          int i, t;
25          while (!q.empty()) q.pop();//清空
26          memset(path, -1, sizeof(path));
27          path[start] = 0, flow[start] = INF;
28          q.push(start);
29          while (!q.empty()){
30                  t = q.front();
31                  q.pop();
32                  if (t == i_end) break;//如果到达终点,则有一条路径,就退出
33                  for (i = 1; i <= m; i++){//从起点开始遍历,记录可以实现的流量。
34                          if (i != start && path[i] == -1 && map[t][i]){
35                                  flow[i] = flow[t] < map[t][i] ? flow[t] : map[t][i];//从起点到当前点可以流过的流量
36                                  q.push(i);
37                                  path[i] = t;
38                 
39             }
40         
41         }
42         
43     }
44          if (path[i_end] == -1) return -1;//增广路径中无法到达终点
45          return flow[m];                   //一次遍历之后的流量增量
46     
47 }
48  int Edmonds_Karp(){
49          int max_flow = 0, step, now, pre;
50          while ((step = bfs()) != -1){          //找不到增路径时退出
51                  max_flow += step;
52                  now = i_end;
53                  while (now != start){
54                          pre = path[now];
55                          map[pre][now] -= step;      //更新正向边的实际容量
56                          map[now][pre] += step;      //添加反向边,反向边进入下一次的增广路径搜索中
57                          now = pre;
58             
59         }
60         
61     }
62          return max_flow;
63     
64 }
65  int main(){
66          int i, u, v, cost;
67          while (scanf_s("%d %d", &n, &m) != EOF){
68                  memset(map, 0, sizeof(map));
69                  for (i = 0; i < n; i++){
70                          scanf_s("%d %d %d", &u, &v, &cost);
71                          map[u][v] += cost;           //not just only one input
72             
73         }
74                  start = 1, i_end = m;
75                  printf("%d\n", Edmonds_Karp());
76         
77     }
78     return 0;
79     
80 }

 

posted @ 2016-11-23 19:34  immoshi  阅读(902)  评论(0编辑  收藏  举报