Cod sursa(job #528803)
#include <stdio.h>
#define maxn 1024
#define oo 1000000
struct nod {
int inf;
nod *next;
} *A[maxn];
int i,N,M,flux,R;
int C[maxn][maxn],cd[maxn],from[maxn];
void citire()
{
int x,y,cost; nod *q;
scanf("%d %d",&N,&M);
for(i=1;i<=M;i++)
{
scanf("%d %d %d",&x,&y,&cost);
q=new nod;
q->inf=y;
C[x][y]=cost;
q->next=A[x];
A[x]=q;
q=new nod;
q->inf=x;
//C[y][x]=0;
q->next=A[y];
A[y]=q;
}
}
int path()
{
int k,min=oo;
for(k=N;k!=1;k=from[k])
if(C[from[k]][k]<min)
min=C[from[k]][k];
return min;
}
void update_fluxuri(int x)
{
int k;
for(k=N;k!=1;k=from[k])
{
C[from[k]][k]-=x;
C[k][from[k]]+=x;
}
}
int bfs()
{
int ps,pi,k; ps=pi=1;
for(i=1;i<=N;i++) from[i]=0;
cd[1]=1;
while(pi>=ps)
{
k=cd[ps];
for(nod *q=A[k];q;q=q->next)
if(from[q->inf]==0 && C[k][q->inf]>0)
{
cd[++pi]=q->inf;
from[q->inf]=k;
}
if(k==N) return 1;
ps++;
}
return 0;
}
int main()
{
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
citire();
R=0;
while(bfs())
{
flux=path();
update_fluxuri(flux);
R+=flux;
}
printf("%d",R);
}