Drainage Ditches HDU - 1532(EK算法&Dinic算法 最大流模板)

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

题意:
池塘到河流之间有一些管道,管道的流率不同,求从池塘排水到河流的最大速率。(最大流模板题)
题目第一行输入m,n,分别表示m条管道和n个节点,其中节点1和n分别表示池塘和河流,之后m行输入a,b,c,表示节点a到节点b的速率为c;
注意:两节点之间可能有多条管道,所以速率要相加

EK算法
代码:

#include"queue"
#include"stdio.h"
#include"string.h"
#include"algorithm"
using namespace std;
#define INF 0x3f3f3f3f
int m,n;
int map[210][210],pre[210],vis[210];
bool bfs(int s,int t)//求s到t之间的一条增广路
{
	int i,u;
	memset(pre,-1,sizeof(pre));
	memset(vis,0,sizeof(vis));
	queue<int>q;
	q.push(s);
	while(!q.empty())
	{
		u=q.front();
		q.pop();
		for(i=1;i<=n;i++)
		{
			if(!vis[i]&&map[u][i]>0)
			{
				vis[i]=1;
				pre[i]=u;//表示i节点的上一个节点是u
				if(i==t)//如果此条增广路能到达汇点(t),返回1
				return 1;
				q.push(i);
			}
		}
	}
	return 0;
}
int EK(int s,int t)
{
	int u,v,i,minn,sum=0;
	while(bfs(s,t))//判断s到t之间是否存在增广路
	{
		u=t;
		minn=INF;
		while(u!=s)//找到此条增广路的中速率的最小值
		{
			v=pre[u];
			if(map[v][u]<minn)
			minn=map[v][u];
			u=v;
		}
		sum+=minn;
		u=t;
		while(u!=s)//更新剩余网络
		{
			v=pre[u];
			map[v][u]-=minn;
			map[u][v]+=minn;
			u=v;
		}
	}
	return sum;
}
int main()
{
	while(~scanf("%d %d",&m,&n))
	{
		int a,b,c,i;
		memset(map,0,sizeof(map));
		for(i=1;i<=m;i++)
		{
			scanf("%d %d %d",&a,&b,&c);
			map[a][b]+=c;//速率相加
		}
		printf("%d\n",EK(1,n));
	}
	return 0;
}

Dinic算法
代码:

#include"queue"
#include"stdio.h"
#include"string.h"
#include"algorithm"
using namespace std;
#define INF 0x3f3f3f3f
int V,E,level[1010];
struct M
{
	int c,f;
}p[1010][1010];
bool bfs()
{
	queue<int>q;
	memset(level,0,sizeof(level));
	q.push(1);
	level[1]=1;
	int u,v;
	while(!q.empty())
	{
		u=q.front();
		q.pop();
		for(v=1;v<=E;v++)
		{
			if(!level[v]&&p[u][v].c>p[u][v].f)
			{
				level[v]=level[u]+1;
				q.push(v);
			}
		}
	}
	return level[E]!=0;
}
int dfs(int u,int cp)
{
	int tmp=cp,v,t;
	if(u==E)
	return cp;
	for(v=1;v<=E&&tmp;v++)
	{
		if(level[u]+1==level[v])
		{
			if(p[u][v].c>p[u][v].f)
			{
				t=dfs(v,min(tmp,p[u][v].c-p[u][v].f));
				p[u][v].f+=t;
				p[v][u].f-=t;
				tmp-=t;
			}
		}
	}
	return cp-tmp;
}
int dinic()
{
	int sum=0,s=0;
	while(bfs())
	{
		while(s=dfs(1,INF))
		sum+=s;
	}
	return sum;
}
int main()
{
	while(~scanf("%d %d",&V,&E))
	{
		int a,b,c;
		memset(p,0,sizeof(p));
		while(V--)
		{
			scanf("%d %d %d",&a,&b,&c);
			p[a][b].c+=c;
		}
		printf("%d\n",dinic());
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值