题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549
#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
#define MAX_VER 20
#define MAX_EDGE 2002
struct node
{
int to,cap,next;
}edge[MAX_EDGE];
int head[MAX_VER],level[MAX_VER],queue[MAX_VER];
int tot,src,dest,visited[MAX_VER];
int getMin(int x,int y)
{
return x<y?x:y;
}
void addEdge(int m)
{
int x,y,val;
tot=0;
memset(head,-1,sizeof(head));
while(m--)
{
scanf("%d %d %d",&x,&y,&val);
edge[tot].to=y;
edge[tot].cap=val;
edge[tot].next=head[x];
head[x]=tot++;
edge[tot].to=x;
edge[tot].cap=0;
edge[tot].next=head[y];
head[y]=tot++;
}
}
void BFS()
{
int v,i,front=0,rear=1;
memset(level,0,sizeof(level));
memset(visited,0,sizeof(level));
queue[0]=src;
level[src]=1;
visited[src]=1;
while(front<rear)
{
v=queue[front++];
for(i=head[v];i!=-1;i=edge[i].next)
{
if(!visited[edge[i].to]&&edge[i].cap>0)
{
level[edge[i].to]=level[v]+1;
visited[edge[i].to]=1;
queue[rear++]=edge[i].to;
}
}
}
}
int DFS(int cur,int maxf)
{
if(cur==dest)
return maxf;
else
{
int i,v,temp,ret=0;
for(i=head[cur];i!=-1&&maxf;i=edge[i].next)
{
v=edge[i].to;
if(edge[i].cap>0&&level[v]==level[cur]+1)
{
temp=DFS(v,getMin(maxf,edge[i].cap));
edge[i].cap-=temp;
edge[i^1].cap+=temp;
maxf-=temp;
ret+=temp;
}
}
return ret;
}
}
int main()
{
int n,m,test,maxflow,cases=1;
scanf("%d",&test);
while(test--)
{
scanf("%d %d",&n,&m);
addEdge(m);
src=1,dest=n;
maxflow=0;
while(1)
{
BFS();
if(!visited[dest])
break;
maxflow+=DFS(src,INF);
}
printf("Case %d: %d\n",cases++,maxflow);
}
return 0;
}