Drainage Ditches

Drainage Ditches

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 4
描述
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. 
输入
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.
输出
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
样例输入
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
样例输出
50

思路:

最大流EK算法, 无限求增广路径, 并每次求出增广量, 直到没有增广路径为止。可以看看最大流视频(很简单易懂), 然后套用模板可解。补充:(最小割:在没有增广路径后, 将各节点分成两个集合(一个含出发点一个含汇点), 所有两集合连接边的可行流之和就是最小割)。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <queue>

#define MAX_NUM 201
#define INF 0xfffffff

using namespace std;

int map[MAX_NUM][MAX_NUM], mark[MAX_NUM], pre[MAX_NUM], augment[MAX_NUM];
int n, m;

int min(int a, int b)
{
	return a > b ? b : a;
}

int bfs(int sp, int ep)
{
	int var, i;
	memset(mark, 0, sizeof(mark));
	memset(pre, -1, sizeof(pre));
	for(i = 1; i <= n; i++)
	{
		augment[i] = INF;
	}
	queue<int> q;
	q.push(sp);
	while(!q.empty())
	{
		var = q.front();
		q.pop();
		if(var == ep)
		{
			break;
		}
		for(i = 1; i <= n; i++)
		{
			if(!mark[i] && map[var][i] > 0)
			{
				mark[i] = 1;
				augment[i] = min(augment[var], map[var][i]);   //到i为止, 最小的增广值
				pre[i] = var;
				q.push(i);
			}
		}
	}
	if(!mark[ep] || sp == ep)
	{
		return 0;								//若没有增广路返回 0
	}
	else
	{
		return augment[ep];                         //返回最小增广值
	}
}

int EK(int sp, int ep)                            //EK算法
{
	int temp, maxflow, augflow, prepos;
	maxflow = 0;
	while((augflow = bfs(sp, ep)) != 0)           //bfs寻找增广路径, 并返回最小增广值
	{
		temp = ep;
		maxflow += augflow;
		while(temp != sp)
		{
			prepos = pre[temp];
			map[prepos][temp] -= augflow;
			map[temp][prepos] += augflow;
			temp = prepos;
		}
	}
	return maxflow;
}

int main()
{
	int i, sp, ep, val;
	while(scanf("%d%d", &m, &n) != EOF)
	{
		memset(map, 0, sizeof(map));                //初始化数组
		memset(augment, 0, sizeof(augment));
		for(i = 0; i < m; i++)
		{
			scanf("%d%d%d", &sp, &ep, &val);
			map[sp][ep] += val;                     //两点间可能有多条边
		}
		printf("%d\n", EK(1, n));
	}
	return 0;
}


你可能感兴趣的:(最大流,EK)