POJ 1273:Drainage Ditches 网络流模板题

本文探讨了如何通过建立排水沟渠系统解决雨水覆盖草地的问题,并利用Edmonds-Karp算法找到从水源到流出地的最大流量。该算法通过寻找增广路径来逐步增加流量,直至无法再找到新的增广路径为止。

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

Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 63339 Accepted: 24434

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 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个水渠,以及每个水渠连接的两个点和水渠的容量。(编号1为源点,编号M为汇点)。求整个网络中最大能流的水的流量。

网络流模板题。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#include <cstring>
#pragma warning(disable:4996)
using namespace std;

const int sum = 201;
const int INF = 99999999;  
int cap[sum][sum],flow[sum][sum],a[sum],p[sum];

int N,M;

void Edmonds_Karp()  
{
	int u,t,start=1,result=0;
	queue <int> s;
	while(s.size())s.pop();

	while(1)
	{
		memset(a,0,sizeof(a));
		memset(p,0,sizeof(p));

		a[1]=INF;
		s.push(1);

		while(s.size())
		{
			u=s.front();
			s.pop();

			for(t=1;t<=M;t++)
			{
				if(!a[t]&&flow[u][t]<cap[u][t])
				{
					s.push(t);
					p[t]=u;
					a[t]=min(a[u],cap[u][t]-flow[u][t]);//要和之前的那个点,逐一比较,到M时就是整个路径的最小残量
				}
			}
		}
		if(a[M]==0)
			break;
		result += a[M];

		for(u=M;u!=1;u=p[u])
		{
			flow[p[u]][u] += a[M];
			flow[u][p[u]] -= a[M]; 
		}
	}
	cout<<result<<endl;
}

int main()
{
	int i,u,v,value;
	while(scanf("%d%d",&N,&M)==2)
	{
		memset(cap,0,sizeof(cap));
		memset(flow,0,sizeof(flow));

		for(i=1;i<=N;i++)
		{
			scanf("%d%d%d",&u,&v,&value);
			cap[u][v] += value;
		}
		Edmonds_Karp();  
	}
	return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/lightspeedsmallson/p/4899594.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值