hdoj 1532 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.  

题意;输入n条河流,m个点,每条河流的流量,求从1到m点的最大流量。


思路:虽然题目很长,但是仔细分析发现其实就是最基础的网络流的最大流问题。正好老师这个星期讲到网络流的问题,就拿这道题练手。残留网络和增广路径等概念就不重复了,不懂的参考课件。定义二维数组Graph[s][t]表示s到t最大的流量,从输入获取流量。然后从起点1开始寻找增广路径。每次寻找用BFS广度优先搜索寻找路径,定义二维数组flow[s][t]表示当前s到t的流量,定义一维数组min_flow[t]表示流过节点t的增广路径中最小的那段流量,假设当前搜索的节点是u,搜索的时候发现当前的流量flow[u][v]小于最大的流量Graph[u][v]的话,v就是下一个搜索的节点,搜索v之后要更新min_flow。如果不能搜索到节点m,说明找不到增广路径,那就结束搜索,把min_flow[m]相加返回结果。如果能搜索到节点m说明找到了增广路径,那就更新flow数组。用一个数组pre记录搜索的上一个节点,从pre[m]逆推到起点更新flow。直到找到所有的增广路径,把所有指向终点的min_flow相加就是整个图的最大流量。


#include<iostream>
#include<vector>
#include<queue>
using namespace std;
#define MAXN 205
int Graph[MAXN][MAXN];
int flow[MAXN][MAXN];
int min_flow[MAXN];
int pre[MAXN];
bool bfs(int m, int s, int t)
{
	memset(min_flow, 0, sizeof(min_flow));
	min_flow[s] = INT_MAX;
	queue<int> q;
	q.push(s);
	while (!q.empty())
	{
		int u = q.front();
		q.pop();
		for (int v = 0; v < m; ++v)
		{
			if (min_flow[v] == 0 && Graph[u][v] > flow[u][v])
			{
				pre[v] = u;
				q.push(v);
				if (min_flow[u] < Graph[u][v] - flow[u][v])
					min_flow[v] = min_flow[u];
				else
					min_flow[v] = Graph[u][v] - flow[u][v];
			}
		}
	}
	if (min_flow[t] == 0)
		return false;
	return true;
}
int maxFlow(int m, int s, int t)
{
	int res = 0;
	memset(pre, 0, sizeof(pre));
	memset(flow, 0, sizeof(flow));
	while (bfs(m, s, t))
	{
		for (int u = t; u != s; u = pre[u])
		{
			flow[pre[u]][u] += min_flow[t];
			flow[u][pre[u]] -= min_flow[t];
		}
		res += min_flow[t];
	}
	return res;
}
int main() {
	int n, m;
	while (cin >> n >> m &&(n || m))
	{
		memset(Graph, 0, sizeof(Graph));
		int si, ei, ci;
		for (int i = 0; i < n; ++i)
		{
			cin >> si >> ei >> ci;
			Graph[si-1][ei-1] += ci;
		}
		int res = maxFlow(m, 0, m - 1);
		cout << res << endl;
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值