和hdu1532没有什么区别,直接上代码:
#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
const int INF=0x3f3f3f3f;
const int Maxn=20;
int pre[Maxn];
int capacity[Maxn][Maxn];
int flow[Maxn];
queue<int> que;
int n,m;
int Min(int a,int b)
{
return a<b?a:b;
}
int BFS(int src ,int des)
{
while(!que.empty())
que.pop();
memset(pre,-1,sizeof(pre));
que.push(src);
pre[src]=0;
flow[src]=INF;
while(!que.empty())
{
int index=que.front();
que.pop();
if(index==des)
break;
for(int i=1;i<=n;i++)
{
if(i!=src&&capacity[index][i]>0&&pre[i]==-1)
{
flow[i]=Min(flow[index],capacity[index][i]);
pre[i]=index;
que.push(i);
}
}
}
if(pre[des]==-1)
return -1;
else
return flow[des];
}
int maxFlow(int src,int des)
{
int increasement=0;
int sumFlow=0;
while((increasement=BFS(src,des))!=-1)
{
int k=des;
while(k!=src)
{
int last=pre[k];
capacity[last][k]-=increasement;
capacity[k][last]+=increasement;
k=last;
}
sumFlow+=increasement;
}
return sumFlow;
}
int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
int cas=1;
while(T--)
{
scanf("%d%d",&n,&m);
memset(capacity,0,sizeof(capacity));
memset(flow,0,sizeof(flow));
int start,end,ci;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&start,&end,&ci);
capacity[start][end]+=ci;
}
printf("Case %d: %d\n",cas,maxFlow(1,n));
cas++;
}
}