原题:http://acm.hdu.edu.cn/showproblem.php?pid=3549
题意:给定n个点,m条边,以及边上的容量,问1到n的最大流;
#include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define inf 999999999;
const int N = 20;
int cap[N][N], flow[N][N]; //cap表示容量,flow表示当前流量;
int a[N], p[N]; //a[i]表示从s到i的最小残量,p表示增广路上一节点;
int n, m;
int bfs(int s, int t)
{
queue<int>q;
memset(flow, 0, sizeof(flow));
int ans = 0;
while(1)
{
memset(a, 0, sizeof(a));
a[s] = inf;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
for(int v = 1;v<=n;v++)
{
if(!a[v] && cap[u][v]>flow[u][v])
{
p[v] = u;
a[v] = min(a[u], cap[u][v]-flow[u][v]);
q.push(v);
}
}
}
if(a[t] == 0)
break;
for(int u = t;u!=s;u = p[u])
{
flow[p[u]][u]+=a[t];
flow[u][p[u]]-=a[t];
}
ans+=a[t];
}
return ans;
}
int main()
{
int cas;
int T = 0;
scanf("%d", &cas);
while(cas--)
{
memset(cap, 0, sizeof(cap));
scanf("%d%d", &n, &m);
for(int i = 1;i<=m;i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
cap[u][v]+=w;
}
printf("Case %d: %d\n", ++T, bfs(1, n));
}
return 0;
}