http://poj.org/problem?id=1273
纯的最大流问题,写作为最大流模板。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<cstring>
using namespace std;
int map[500][500];
int ek(int s,int t,int m)//from s to t
{
int ans=0,i,j;
while (1)
{
int pre[500],p[500],h1=0,h2=0;//pre数组保存路径
h2++;
p[h2]=s;
for (i=0;i<=m;i++) pre[i]=-1;
while (h1<h2)
{
h1++;
int v=p[h1];
for (i=1;i<=m;i++)
if ((map[v][i]>0) && (pre[i]==-1))
{
pre[i]=v;
h2++;
p[h2]=i;
if (i==t) break;
}
if (pre[t]==-1) break; //未找到增广路径
int min=9999999;
for (i=t;i!=s;i=pre[i])
if (map[pre[i]][i]<min) min=map[pre[i]][i];
ans+=min;
for (i=t;i!=s;i=pre[i])//修改残留网络
{
map[pre[i]][i]-=min;
map[i][pre[i]]+=min;
}
}
return ans;
}
int main()
{
int n,m,i,j,k,e;//m:the number of points
while (1)
{
if (scanf("%d%d",&n,&m)==EOF) break;
memset(map,0,sizeof(map));
for (i=1;i<=n;i++)
{
scanf("%d%d%d",&j,&k,&e);
map[j][k]+=e;
}
cout<<ek(1,m,m)<<endl;
}
return 0;
}