poj_1273Drainage Ditches

Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 47664   Accepted: 17948

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

网络流第一题,参考刘汝佳的白书

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<cmath>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<queue>
#include <stack>
using namespace std;
#pragma warning(disable : 4996)
const int MAXN = 250;
const int INF = 0x7FFFFFFF;
int flow[MAXN][MAXN], cap[MAXN][MAXN];
int maxflow, n, m;

void EKarp(int s, int e)
{
	queue<int>Q;
	int u, v, a[MAXN], father[MAXN];
	maxflow = 0;
	memset(flow, 0, sizeof(flow));
	while (true)
	{
		memset(a, 0, sizeof(a));
		a[s] = INF;
		Q.push(s);
		while (!Q.empty())//BFS找增广路
		{
			int u = Q.front();
			Q.pop();
			for (v = 1; v <= n; v++)
			{
				if(a[v] == 0 && cap[u][v] > flow[u][v])//找到新节点v
				{
					father[v] = u;//记录v的父亲,并加入FIFO队列
					Q.push(v);
					a[v] = min(a[u], cap[u][v] - flow[u][v]);//s-v路径上的最小残量
				}
			}
		}
		if(a[e] == 0)//找不到,则当前流已经是最大流
		{
			break;
		}
		for (u = e; u != s; u = father[u])//从汇点往回走
		{
			flow[father[u]][u] += a[e];//更新正向流量
			flow[u][father[u]] -= a[e];//更新反向流量
		}
		maxflow += a[e];//更新从s流出的总流量
	}
}

int main()
{
	freopen("in.txt", "r", stdin);
	int x, y, z;
	while(scanf("%d %d", &m, &n) != EOF)
	{
		memset(cap, 0, sizeof(cap));
		while (m--)
		{
			scanf("%d %d %d", &x, &y, &z);
			cap[x][y] += z;
		}
		EKarp(1, n);
		printf("%d\n", maxflow);
	}
	return 0;
}


原文地址:https://www.cnblogs.com/lgh1992314/p/5834997.html