题意:赤果果的网络流
代码:
/*
* 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;
}