HDU 3549 网络流水题

题意:赤果果的网络流

代码:

/*
*  Author:      illuz <iilluzen[at]gmail.com>
*  Blog:        http://blog.csdn.net/hcbbt
*  File:        hdu3549.cpp
*  Create Date: 2013-12-04 09:28:46
*  Descripton:  max flow, basic, bfs
*/

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;

const int MAXN = 16;
const int INF = 0x3c3c3c3c;

int mr[MAXN];	// 从源点到i的最小残量
int p[MAXN];	// 更新时i的上流节点
int c[MAXN][MAXN], f[MAXN][MAXN];	// capitar and flow

int maxFlow(int op, int ed, int n)	// start, end, num of points
{
	queue<int> q;
	memset(f, 0, sizeof(f));
	memset(p, 0, sizeof(p));
	int F = 0;	// total flow

	// bfs
	while (1) {
		memset(mr, 0, sizeof(mr));
		q.push(op);
		mr[op] = INF;	// 源点残量
		while (!q.empty()) {
			int u = q.front();
			q.pop();
			for (int v = 0; v < n; v++) 
				if (!mr[v] && c[u][v] > f[u][v]) {
					p[v] = u;
					q.push(v);
					mr[v] = min(mr[u], c[u][v] - f[u][v]);
				}
		}

		if (mr[ed] == 0) return F;

		// update
		for (int u = ed; u != op; u = p[u]) {
			f[u][p[u]] -= mr[ed];
			f[p[u]][u] += mr[ed];
		}
		F += mr[ed];
	}
}

int main() {
	int t, nn, en, s, e, cast;
	scanf("%d", &t);
	for (int cas = 0; cas < t; cas++) {
		scanf("%d%d", &nn, &en);
		memset(c, 0, sizeof(c));

		for (int i = 0; i < en; i++) {
			scanf("%d%d%d", &s, &e, &cast);
			c[s - 1][e - 1] += cast;
		}

		printf("Case %d: %d\n", cas + 1, maxFlow(0, nn - 1, nn));
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值