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更加方便。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值