#include <iostream>
#include <queue>
using namespace std;
#define min(a,b) (a)<(b)?a:b
#define N 16
int capacity[N][N]; //容量
int flow[N]; //残余容量
int pre[N]; //前驱结点
int n,m;
queue<int> qu;
int BFS(int src,int des)
{
while(!qu.empty())
qu.pop();
for(int i=1;i<=n;i++)
pre[i]=-1;
pre[src]=0;
flow[src]=INT_MAX; //初始化源点的流量为无穷
qu.push(src);
while(!qu.empty())
{
int index=qu.front();
qu.pop();
if(index == des)
break;
for(i=1;i<=n;i++)
{
if(i!=src && capacity[index][i]>0 && pre[i] == -1)
{
pre[i]=index;
flow[i]=min(capacity[index][i],flow[index]);
//一步步找出增广路径中的最小的可增加流量
qu.push(i);
}
}
}
if(pre[des] == -1)
return -1; //不存在增广路径
return flow[des];
}
int MaxFlow(int src,int des)
{
int aug=0;
int sumflow=0;
while((aug=BFS(src,des)) != -1)
{
int k=des;
while(k!=src)
{
int last=pre[k];
capacity[last][k] -= aug;
capacity[k][last] += aug;
k=last;
}
sumflow += aug;
}
return sumflow;
}
int main()
{
int nCase,cas=1;
cin>>nCase;
while(nCase--)
{
cin>>n>>m;
memset(capacity,0,sizeof(capacity));
memset(flow,0,sizeof(flow));
for(int i=0;i<m;i++)
{
int u,v,w;
cin>>u>>v>>w;
if(u == v)
continue;
capacity[u][v] +=w;
}
cout<<"Case "<<cas++<<": "<<MaxFlow(1,n)<<endl;
}
return 0;
#include <queue>
using namespace std;
#define min(a,b) (a)<(b)?a:b
#define N 16
int capacity[N][N]; //容量
int flow[N]; //残余容量
int pre[N]; //前驱结点
int n,m;
queue<int> qu;
int BFS(int src,int des)
{
while(!qu.empty())
qu.pop();
for(int i=1;i<=n;i++)
pre[i]=-1;
pre[src]=0;
flow[src]=INT_MAX; //初始化源点的流量为无穷
qu.push(src);
while(!qu.empty())
{
int index=qu.front();
qu.pop();
if(index == des)
break;
for(i=1;i<=n;i++)
{
if(i!=src && capacity[index][i]>0 && pre[i] == -1)
{
pre[i]=index;
flow[i]=min(capacity[index][i],flow[index]);
//一步步找出增广路径中的最小的可增加流量
qu.push(i);
}
}
}
if(pre[des] == -1)
return -1; //不存在增广路径
return flow[des];
}
int MaxFlow(int src,int des)
{
int aug=0;
int sumflow=0;
while((aug=BFS(src,des)) != -1)
{
int k=des;
while(k!=src)
{
int last=pre[k];
capacity[last][k] -= aug;
capacity[k][last] += aug;
k=last;
}
sumflow += aug;
}
return sumflow;
}
int main()
{
int nCase,cas=1;
cin>>nCase;
while(nCase--)
{
cin>>n>>m;
memset(capacity,0,sizeof(capacity));
memset(flow,0,sizeof(flow));
for(int i=0;i<m;i++)
{
int u,v,w;
cin>>u>>v>>w;
if(u == v)
continue;
capacity[u][v] +=w;
}
cout<<"Case "<<cas++<<": "<<MaxFlow(1,n)<<endl;
}
return 0;