最大流模板题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
int n,m;
int g[20][20];
int pre[20];
int visit[20];
int Augment(){
memset(pre,0,sizeof(pre));
memset(visit,0,sizeof(visit));
pre[1]=0;
visit[1]=1;
queue <int> q;
q.push(1);
bool bfindpath=false;
while(!q.empty()){
int v=q.front();
q.pop();
for(int i=1;i<=20;i++){
if(g[v][i]>0&&visit[i]==0){
pre[i]=v;
visit[i]=1;
if(i==m){
bfindpath=true;
while(!q.empty())
q.pop();
break;
}
else
q.push(i);
}
}
}
if(!bfindpath)
return 0;
int minflow=1000000000;
int v=m;
while(pre[v]){
minflow=min(g[pre[v]][v],minflow);
v=pre[v];
}
v=m;
while(pre[v]){
g[pre[v]][v]-=minflow;
g[v][pre[v]]+=minflow;
v=pre[v];
}
return minflow;
}
int main(){
int t;
scanf("%d",&t);
int cnt=1;
while(t--){
memset(g,0,sizeof(g));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
g[a][b]+=c;
}
int ans=0;
int aug;
while(aug=Augment())
ans+=aug;
printf("Case %d: %d\n",cnt++,ans);
}
return 0;
}