这应该是最裸的最大流题了吧,刚开始学最大流,拿来练练手。从源点到汇点的最大流,EK算法实现如下,不再多说:http://acm.hdu.edu.cn/showproblem.php?pid=3549
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
#define INS 0xfffffff
#define MAX 105
int map[MAX][MAX],f[MAX];
int r[MAX];
bool vis[MAX];
int m,n;
bool bfs(){
int start,end,size,begin = 0;
start = end = 0; size = 100;
memset(vis,false,sizeof(vis));
r[start] = 1; int k = 1;
while(start <= end){
vis[ r[start] ] = true;
for(int i = 1;i <= n;++i){
if(!vis[i] && map[ r[start] ][i] > 0){
end = (end+1)%size;
f[i] = r[start]; r[end] = i;
if(i == n) return true;
}
}
start = (start+1)%size;
}
return false;
}
int E_K(){
int d = INS,i,res = 0;
memset(f,-1,sizeof(f));
f[1] = 0;
while(bfs()){
i = n; d = INS;
while(f[i] != 0){
d = min(d,map[ f[i] ][i]); i = f[i];
}
i = n; res += d;
while(f[i] != 0){
map[ f[i] ][i] -=d;
map[i][ f[i] ] +=d;
i = f[i];
}
memset(f,-1,sizeof(f)); f[1] = 0;
} return res;
}
int main()
{
int t;
scanf("%d",&t);
for(int k = 1;k <= t;++k){
scanf("%d%d",&n,&m);
int b,e,v;
memset(map,0,sizeof(map));
for(int i = 0;i < m;++i){
scanf("%d%d%d",&b,&e,&v);
map[b][e] += v;
}
printf("Case %d: %d\n",k,E_K());
}
return 0;
}