在广搜的基础上设计的算法。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
const int num_p=200;
const int INF=1e9;
int flow[num_p][num_p],cap[num_p][num_p];
int p[num_p],a[num_p],n,m;
int EK()
{
queue<int>q;
memset(flow,0,sizeof(flow));
int f=0;
int s=0,t=n-1;
for(;;)
{
memset(a,0,sizeof(a));
a[s]=INF;
q.push(s);
while(!q.empty())
{
int u=q.front();
// printf("u=%d\n",u);
q.pop();
for(int v=1; v<=n; v++)
if(!a[v]&&cap[u][v]>flow[u][v])
{
// printf("p[%d]=%d\n",v,u);
p[v]=u;
// printf("push(%d)\n",v);
q.push(v);
// printf("a[%d]=%d\tcap[%d][%d]-flow[%d][%d]=%d\n",u,a[u],u,v,u,v,cap[u][v]-flow[u][v]);
a[v]=min(a[u],cap[u][v]-flow[u][v]);
// printf("a[%d]=%d\n",v,a[v]);
}
}
// printf("a[%d]=%d\n",t,a[t]);
if(a[t]==0)
break;
for(int u=t; u!=s; u=p[u])
{
flow[p[u]][u]+=a[t];
// printf("flow[%d][%d]=%d\n",p[u],u,flow[p[u]][u]);
flow[u][p[u]]-=a[t];
}
f+=a[t];
}
return f;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0; i<m; i++)
{
int ua,vb,cc;
scanf("%d%d%d",&ua,&vb,&cc);
cap[ua][vb]=cc;
}
int ans= EK();
cout<<ans<<endl;
}
return 0;
}
/*
6 10
0 1 16
0 2 13
1 2 10
2 1 4
1 3 12
3 2 9
2 4 14
3 5 20
4 3 7
4 5 4
*/