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

本文介绍了一种经典图论问题——最大流问题,以及两种解决该问题的算法:EK算法(Edmonds-Karp算法)和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;
}
提供了一个基于51单片机的RFID门禁系统的完整资源文件,包括PCB图、原理图、论文以及源程序。该系统设计由单片机、RFID-RC522频射卡模块、LCD显示、灯控电路、蜂鸣器报警电路、存储模块和按键组成。系统支持通过密码和刷卡两种方式进行门禁控制,灯亮表示开门成功,蜂鸣器响表示开门失败。 资源内容 PCB图:包含系统的PCB设计图,方便用户进行硬件电路的制作和调试。 原理图:详细展示了系统的电路连接和模块布局,帮助用户理解系统的工作原理。 论文:提供了系统的详细设计思路、实现方法以及测试结果,适合学习和研究使用。 源程序:包含系统的全部源代码,用户可以根据需要进行修改和优化。 系统功能 刷卡开门:用户可以通过刷RFID卡进行门禁控制,系统会自动识别卡片并判断是否允许开门。 密码开门:用户可以通过输入预设密码进行门禁控制,系统会验证密码的正确性。 状态显示:系统通过LCD显示屏显示当前状态,如刷卡成功、密码错误等。 灯光提示:灯亮表示开门成功,灯灭表示开门失败或未操作。 蜂鸣器报警:当刷卡或密码输入错误时,蜂鸣器会发出报警声,提示用户操作失败。 适用人群 电子工程、自动化等相关专业的学生和研究人员。 对单片机和RFID技术感兴趣的爱好者。 需要开发类似门禁系统的工程师和开发者。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值