POJ 1273 Drainage Ditches (最大流问题——Edmonds-Karp算法)

一.最大流问题

最大流问题最形象的比喻就是给定一个有向图G(V,E),每个边有一定的权值表示边的容量,再给定起点s和终点t,s处有水不断的流出,t是一个蓄水池,问最多有多少水从s流进了t?

二.Edmonds-Karp算法

简单来说这个算法就是不断重复这样的操作:

1.从图中找到s到t的路径(为了减小时间复杂度使用bfs来寻找最短的一条路径),如果找不到路径,那么就结束。

2.找到的该路径所有边容量的最小值,这是s通过这条路径到达t的最大值,称为nMinFlow

3.从这条路径的每一个边上的容量中减去nMinFlow,再从反向的边上加上nMinFlow

4.把这次bfs找到的nMinFlow加到总的最大流MaxFlow中

5.再次重复步骤1

三.题目:Drainage Ditches

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 <vector>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
int map[210][210];
int vis[210];
int prev[210];
int n,m;
int Augment()
{
	bool flag=0;
	deque <int> q;
	q.clear();
	memset(vis,0,sizeof(vis));
	memset(prev,0,sizeof(prev));
	q.push_back(1);
	vis[1]=1;
	prev[1]=0;
	while(!q.empty())
	{
		int v=q.front();
		q.pop_front();
		for(int i=1;i<=m;i++)
		{
			if(vis[i]==0&&map[v][i]>0)
			{
				q.push_back(i);
				vis[i]=1;
				prev[i]=v;
				if(i==m)
				{
					flag=1;
					q.clear();
					break;
				}
			}
		}
	}
	if(!flag) return 0;
	int nMinFlow=99999999;
	int v=m;
	while(prev[v])
	{
		nMinFlow=min(nMinFlow,map[prev[v]][v]);
		v=prev[v];
	}
	v=m;
	while(prev[v])
	{
		map[prev[v]][v]-=nMinFlow;
		map[v][prev[v]]+=nMinFlow;
		v=prev[v];
	}
	return nMinFlow;
}
int main()
{
	//freopen("input.txt","r",stdin);
	while(cin>>n>>m)
	{
		memset(map,0,sizeof(map));
		int s,e,c;
		for(int i=1;i<=n;i++)
		{
			cin>>s>>e>>c;
			map[s][e]+=c;//两点之间也许有多条边连接,一定要注意这个情况;
		}
		int maxflow=0,aug=0;
		while(aug=Augment())
		{
			maxflow+=aug;
		}
		cout<<maxflow<<endl;
	}
	return  0;
}

五.Debug总结

1.首先注意一个问题,stl中的queue是没有clear函数的,所以以后还是使用deque更加方便。

时间: 2024-10-05 22:40:11

POJ 1273 Drainage Ditches (最大流问题——Edmonds-Karp算法)的相关文章

POJ 1273 Drainage Ditches(网络流dinic算法模板)

POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdio.h> #include <algorithm> #include <queue> #include <string.h> /* POJ 1273 dinic算法模板 边是有向的,而且存在重边,且这里重边不是取MAX,而是累加和 */ using namespace

POJ 1273 Drainage Ditches(我的EK算法模板)

题意:给你n条边,目标位置t:接下来是每条边,包括起点,终点,容量: 感想:第一道最大流的代码,这道题是我更深地理解了Ek算法,不过这道题有个超坑的情况,那就是出现重边的情况==! 思路:EK算法 AC代码: #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std; #define INF 100000000 #define N

POJ 1273 Drainage Ditches 最大流

很裸的最大流问题,不过注意会有重边,o(╯□╰)o,被阴了WA了一发 还有就是要用long long #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include

POJ 1273 Drainage Ditches 网络流基础

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

POJ 1273 Drainage Ditches(初识网络流)

开始研究网络流了,看了两个晚上吧,今天总算动手实践一下,有了更深的理解 总结一下:在最大流中,容量与实际流量满足3点: 1.实际流量<=容量 2.任意两点之间   : 流量(a->b)==流量(b->a) 3.流量守恒原则   :从s流出的流量 == t流入的流量 一.为什么叫增广路,因为在所有的流量网络中,会存在一个残量,所以在整个残量网络中,找到一个最小值,加到所有的流量线路里,便叫增广. 二.为什么要修改反向流量,因为在更新流量网时,当前选择的并不一定就是最优解,比如u->v

POJ 1273 Drainage Ditches(网络流 最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55893   Accepted: 21449 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

POJ 1273 Drainage Ditches (网络最大流)

http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55235   Accepted: 21104 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means

hdu 1532 poj 1273 Drainage Ditches (最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55276   Accepted: 21122 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

poj 1273 Drainage Ditches(最大流)

http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 62708   Accepted: 24150 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means