#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int N=1024*2;
const int inf=1<<24;
struct arc
{
int c,f;
} g[N][N];
int n,m,i,j,u,v,c,f;
int abs(int x)
{
if(x<0) return -x;
else return x;
}
void ford()
{
int p[N],a[N],flag[N];
queue<int>q;
while(1)
{
memset(p,0xff,sizeof(p));
memset(a,0xff,sizeof(a));
memset(flag,0xff,sizeof(flag));
while(!q.empty()) q.pop();
q.push(0);
flag[0]=0;
p[0]=0;
a[0]=inf;
while(!q.empty()&&flag[n-1]==-1)
{
u=q.front();
q.pop();
for(i=0; i<n; i++)
{
if(flag[i]==-1)
{
if(g[u][i].c<inf&&g[u][i].f<g[u][i].c)
{
flag[i]=0;
p[i]=u;
a[i]=min(a[u],g[u][i].c-g[u][i].f);
q.push(i);
}
else if(g[i][u].c<inf&&g[i][u].f>0)
{
flag[i]=0;
p[i]=-u;
a[i]=min(a[u],g[i][u].f);
q.push(i);
}
}
}
flag[u]=1;
}
//printf("%d %d
",flag[n-1],a[n-1]);
if(flag[n-1]==-1||a[n-1]==0) break;
int k1=n-1,k2=abs(p[k1]);
int add=a[n-1];
while(1)
{
if(g[k2][k1].f<inf)
g[k2][k1].f+=add;
else
g[k1][k2].f-=add;
if(k2==0) break;
k1=k2;
k2=abs(p[k2]);
}
}
int flow=0;
for(i=0; i<n; i++)
if(g[0][i].f<inf)
flow+=g[0][i].f;
printf("%d
",flow);
}
int main()
{
int _;
scanf("%d",&_);
for(int k=1; k<=_; k++)
{
scanf("%d%d",&n,&m);
for(i=0; i<n; i++)
for(j=0; j<n; j++)
g[i][j].c=g[i][j].f=inf;
for(i=0; i<m; i++)
{
scanf("%d%d%d",&u,&v,&c);
u--;
v--;
if(g[u][v].c==inf)
g[u][v].c=c;
else
g[u][v].c+=c;
g[u][v].f=0;
}
printf("Case %d: ",k);
ford();
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/