网络流问题 最大流 ford-fulkerson算法 edmonds-karp算法

来源:互联网 发布:学生作业管理系统源码 编辑:程序博客网 时间:2024/05/20 12:48

可行流:

       即多条弧的集合,且每条弧的流量为非负的,且不超过该弧的容量;流入原点的流量等于流出终点的流量。

最大流:

        即网络中的流量最大的可行流。

 

 

最大流的求法:即由原网络得到残量网络,再找到残量网络中的增广路,根据增广路求最大流。

步骤:

     1. 找到残量网络中的一条增广路。

     2. 求出该增广路上的最小的流量值,使各个弧的容量都减去这个值,并各个反向弧都加上这个值。

     3. 重复1,2 直到找不到增广路。将每一次的流量值相加,结果为最大流。

 

p s. ford-fulkerson 算法在1中用的是深度搜索

      edmonds-karp 算法在1中用到广度搜索。

 

 

 

A - Drainage Ditches
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Submit Status

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 41 2 401 4 202 4 202 3 303 4 10

Sample Output

50

 

 

 

代码:

 

#include<iostream>#include <stdio.h>#include <string.h>#include <queue>using namespace std;int n;int s, t;int c[201][201];int f[201][201];int p[201];bool v[201];bool aug(){//memset(p,0,sizeof(p));queue<int>q;q.push(s);memset(v,0,sizeof(v));v[s] = 1;while (!q.empty()){int qf = q.front();q.pop();for (int i = 1; i <= n; ++ i){if (!v[i] && c[qf][i]){v[i] = 1;p[i] = qf;q.push(i);if (i == t)return 1;}}}return 0;}int edmonds(int s,int t){int res = 0;memset(f,0,sizeof(f));while (1){if (!aug()) break;int i = t;int mr = (1<<30);while (i != s){if (mr > c[p[i]][i])mr = c[p[i]][i];i = p[i];}i = t;while (i != s){c[p[i]][i] -= mr;c[i][p[i]] += mr;i = p[i];}res += mr;}return res;}int main(){int e;while (cin>>e>>n){s = 1; t = n;memset(c,0,sizeof(c));for(int k=0;k<e;k++){int a1, a2, a3;scanf("%d %d %d", &a1, &a2, &a3);c[a1][a2] += a3;}printf("%d\n", edmonds(1,n));}return 0;}

 

原创粉丝点击