POJ 1273 Drainage Ditches 最大流

49 篇文章 0 订阅

http://poj.org/problem?id=1273
题目大意:
给你N条路径(有重边),和M个点,求以1为源点,M为汇点的最大流。
思路:
第一题最大流问题,直接用Edmonds-Karp算法即可


#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN = 1<<8;
int map[MAXN][MAXN];
int flow[MAXN];
int pre[MAXN];
int n, m;
int bfs(int s,int t)
{
	memset(pre, -1, sizeof(pre));
	queue<int> q;
	q.push(s);
	pre[s] = 0;
	flow[s] = 0x3ffffff;

	while (!q.empty())
	{
		int cur = q.front();
		q.pop();
		for (int i = 1; i <= m; i++) 
		{
			if (map[cur][i] > 0 && pre[i] == -1)
			{
				flow[i] = min(flow[cur],map[cur][i]);
				pre[i] = cur;
				q.push(i);
			}
		}
	}
	return pre[t] == -1 ? -1 : flow[t];
}


int maxFlow(int s,int t)
{
	int ans = 0;
	int curFlow=0;
	while ((curFlow=bfs(s,t))  !=-1)
	{
		int cur = t;
		while (cur != s)
		{
			map[pre[cur]][cur] -= curFlow;
			map[cur][pre[cur]] += curFlow;
			cur = pre[cur];
		}
		ans += curFlow;
	}
	return ans;
}

int main()
{
	while (~scanf("%d%d", &n, &m))
	{
		memset(map, 0, sizeof(map));
		for (int i = 0; i < n; i++)
		{
			int from, to, val;
			scanf("%d%d%d", &from, &to, &val);
			map[from][to] += val;
		}

		printf("%d\n", maxFlow(1,m));
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值