Pagini recente » Cod sursa (job #424766) | Cod sursa (job #2053832) | Cod sursa (job #1962799) | Diferente pentru autumn-warmup-2007/solutii/runda-2 intre reviziile 56 si 37 | Cod sursa (job #1074953)
#include <stdio.h>
#include <vector>
#define fr(i,a,b) for(int i=a;i<b;++i)
#define p P[n]
#define N 1000
using namespace std;
vector<int>o[N];
int c[N][N],f[N][N];
int L[N],l;
int Q[N],q;
int P[N];
int v,e;
int main(){
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
scanf("%i%i",&v,&e);
int x,y,z;
fr(i,0,e){
scanf("%i%i%i",&x,&y,&z);
o[x-1].push_back(y-1);
o[y-1].push_back(x-1);
c[x-1][y-1]=z;
}
int F=0;
while(1){
q=0;
Q[q++]=0;
L[0]=++l;
fr(i,0,q){
int b=Q[i];
if(b==v-1) continue;
int s=o[b].size();
fr(j,0,s){
int e=o[b][j];
if(c[b][e]==f[b][e]||L[e]==l) continue;
L[e]=l;
Q[q++]=e;
P[e]=b;
}
}
if(L[v-1]<l) break;
int s=o[v-1].size();
fr(i,0,s){
int e=o[v-1][i];
if(c[e][v-1]==f[e][v-1]||L[e]<l) continue;
P[v-1]=e;
int m=-1;
for(int n=v-1;n;n=p) if(m==-1||m>c[p][n]-f[p][n]) m=c[p][n]-f[p][n];
if(m<=0)continue;
for(int n=v-1;n;n=p) f[p][n]+=m,f[n][p]-=m;
F+=m;
}
}
printf("%i",F);
return 0;
}