Pagini recente » Cod sursa (job #331057) | Cod sursa (job #619866) | Cod sursa (job #1122605) | Cod sursa (job #2495884) | Cod sursa (job #2986844)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("maxflow.in");
ofstream cout("maxflow.out");
const int MAX = 1e3 + 1;
const int inf = 1e9 + 1;
int c[MAX][MAX] , f[MAX][MAX] , n , m , x , y , cap , level[MAX] , ef[MAX];
vector <int> g[MAX];
bool bfs(){
queue <int> q;
for(int i = 1 ; i <= n ; i++){
level[i] = ef[i] = 0;
}
q.push(1);
level[1] = 1;
while(!q.empty()){
int x = q.front();
q.pop();
for(auto it : g[x]){
if(!level[it] && (c[x][it]-f[x][it])>0){
level[it] = level[x]+1;
if(it == n) return 1;
q.push(it);
}
}
}
return 0;
}
int dfs( int x , int flow ){
if(x == n){
return flow;
}
int sz = g[x].size();
for(; ef[x] < sz ; ef[x]++){
int it = g[x][ef[x]];
if(level[it] == level[x] + 1 && (c[x][it]-f[x][it] > 0)){
int new_flow = dfs(it,min(flow,c[x][it]-f[x][it]));
if(new_flow){
f[x][it] += new_flow;
f[it][x] -= new_flow;
return new_flow;
}
}
}
return 0;
}
int main(){
cin >> n >> m;
while(m--){
cin >> x >> y >> cap;
g[x].push_back(y);
g[y].push_back(x);
c[x][y] = cap;
}
int flow = 0;
while(bfs()){
while(1){
int x = dfs(1,inf);
if(!x) break;
flow+=x;
}
}
cout << flow;
return 0;
}