图论_最大流_基础_nyoj 323_Drainage Ditches

本文介绍了一个典型的最大流问题——DrainageDitches,并提供了完整的代码实现。该问题通过建立排水沟系统来解决农场积水问题,利用Dinic算法实现了水源到汇点的最大流量计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Drainage Ditches

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 4
描述
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. 
输入
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.
输出
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
样例输入
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
样例输出
50
来源
USACO 93
上传者

张洁烽

#include<string.h>
#include<queue>
#include<iostream>
#include<cstdio> 
using namespace std;
const int VN=220;
const int INF=0x3f3f3f3f;
int n,m;
int src;//源点 
int des;//汇点 
int map[VN][VN];
int dep[VN];//表示当前点到起点src的层数 

int BFS()//重新建图(按层数建图) 
{
	queue<int>q;
	while(!q.empty())//初始化队列 
		q.pop(); 
	memset(dep,-1,sizeof(dep));
	dep[src]=0;
	q.push(src);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int v=1;v<=m;v++)//实际的边的数组下标1开始 
		if(map[u][v]>0&&dep[v]==-1)
		{
			dep[v]=dep[u]+1;
			q.push(v);
		}
    }
    return dep[des]!=-1;//判断是否有通向汇点的路 
}
int DFS(int u,int minx)
{//查找路径上最小流量 
	if(u==des)
		return minx;
	int tmp;
	for(int v=1;v<=m;v++)
	if(map[u][v]>0&&dep[v]==dep[u]+1&&(tmp=DFS(v,min(minx,map[u][v])))) 
	{
		map[u][v]-=tmp;//正向减少 
		map[v][u]+=tmp;//反向增加 
		return tmp;
	}
	return 0; 
}
int Dinic()
{
	int ans=0,tmp;
	while(BFS())
	{
		while(1)
		{
			tmp=DFS(1,INF);
			if(tmp==0)
				break;
			ans+=tmp; 
		 } 
	}
	return ans; 
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		memset(map,0,sizeof(map));
		int u,v,w;
		for(int i=0;i<n;i++)
		{
			scanf("%d%d%d",&u,&v,&w);
			map[u][v]+=w; //防止有重边 
		}
		src=1,des=m;
		printf("%d\n",Dinic()); 
	}
	return 0;
}

kk_疯言疯语_碎碎念

这道题是最大流的基础题,看了书上写的模板,很复杂,没看懂,就直接找的大神的博客看代码。看第一遍知道大致的流程。

看好几遍之后,知道要反复在Dinic()函数中进行bfs 和dfs两位大佬的调用

调用BFS()建图  /***注意,这里建图是按一层一层建的,就是源点src到每个顶点的层次,是直接到的还是间接到的,用数组dep[i]标记号***/  

再用DFS()搜索出实际的流通量,dfs()中的递归调用,也是有了bfs()按层建图的基础来实现的,一层一层的去搜,要是遇到汇点了,就返回每次搜索时比较出来的最小流通量minx的值。/***注意:搜索也不是白搜的,搜到了最小流通量后还要把map[u][v]和map[v][u]的值都改了,也就是当前源点到下一个顶点的流量,流过了要把这条边可以流的容量给改一下,map[u][v]-tmp,至于map[v][u]+tmp是为啥?我的理解就是把有向图u-->v单向 的路程变成无向图v-->u,有来就能有回。***/

要是按正常的思路来想也是挺对的,每条表都有自己的容量(最大的流通量),怎么才能让 源点s,到汇点des 的流量最大呢?

就是找遍每一条可行的路径,每当你找到一条路径的时候,源点到汇点的最大流通量却是这条路径上每个边中的最小流通量minx(就算其他边能承受的范围再多,最小容量的边承受不了也是会溢出的);之后呢,就把这条路线上的每个边的容量减去minx;这样一直循环,直到所有的路径都遍历了一遍,把每条路径上的minx 加起来就是最大流了。

      


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值