题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549
简单明了的最大流问题,就当熟练模板了。
记得补题解的孩子都是好孩子。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define MAX 65535
#define Max 10000
using namespace std;
struct node
{
int e;
int w;
int fro;
}eg[Max << 1];
int head[Max << 1];
int cont;
int add(int s,int e,int w)
{
eg[cont].e = e;
eg[cont].w = w;
eg[cont].fro = head[s];
head[s] = cont++;
eg[cont].e = s;
eg[cont].w = 0;
eg[cont].fro = head[e];
head[e] = cont++;
return 0;
}
int dis[Max << 1];
int BFS(int s,int e)
{
queue<int> que;
int now;
memset(dis,-1,sizeof (dis));
dis[s] = 0;
que.push(s);
while (!que.empty())
{
now = que.front();
que.pop();
for (int i = head[now];i != -1;i = eg[i].fro)
{
int v = eg[i].e;
if (dis[v] == -1 && eg[i].w > 0)
{
dis[v] = dis[now] + 1;
que.push(v);
}
}
}
if (dis[e] != -1)
return 1;
return 0;
}
int dinic(int s,int e,int t)
{
if (s == e)
return t;
int tmp = t;
for (int i = head[s];i != -1;i = eg[i].fro)
{
int v = eg[i].e;
if (dis[v] == dis[s] +1 && eg[i].w > 0)
{
int imin = dinic(v,e,min(t,eg[i].w));
eg[i].w -= imin;
eg[i ^ 1].w += imin;
t -= imin;
}
}
return tmp - t;
}
int main()
{
int n,m;
int N;
int cas = 1;
scanf ("%d",&N);
while (N--)
{
int i,k;
cont = 0;
memset(head,-1,sizeof (head));
scanf ("%d%d",&n,&m);
for (i = 0;i < m;i++)
{
int s,e,w;
scanf ("%d%d%d",&s,&e,&w);
add(s,e,w);
}
int ans = 0;
while (BFS(1,n))
ans += dinic(1,n,MAX);
printf ("Case %d: %d\n",cas++,ans);
}
return 0;
}