Pagini recente » Cod sursa (job #2261400) | Cod sursa (job #2433173) | Cod sursa (job #2942296) | Cod sursa (job #2805728) | Cod sursa (job #2818859)
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
#define nmax 1005
#define cmax 110005
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
int c[nmax][nmax], fl[nmax][nmax];
vector <int> la[nmax];
int main()
{
int n,m,i,x,y,cap,flow=0;
f>>n>>m;
for(i=0; i<m; i++){
f>>x>>y>>cap;
la[x].push_back(y);
c[x][y] = cap;
}
int nc;
int p[n+1]={0};
int ok=0;
while(ok==0){
int p[n+1]={0};
queue <int> q;
q.push(1);
while(!q.empty()){
nc = q.front();
q.pop();
for(i=0; i<la[nc].size(); i++){
int naux = la[nc][i];
if(c[nc][naux]>fl[nc][naux]){
p[naux]=nc;
q.push(naux);
}
}
}
if(p[n]!=0){
int fm=cmax;
int t;
nc = n;
while(nc!=1){
t=p[nc];
if(c[t][nc]-fl[t][nc]<fm) fm = c[t][nc]-fl[t][nc];
nc=t;
}
nc = n;
while(nc!=1){
t=p[nc];
fl[t][nc] += fm;
nc=t;
}
flow += fm;
}
if(p[n]==0) ok=1;
}
g<<flow;
f.close();
g.close();
return 0;
}