Pagini recente » Cod sursa (job #2080835) | Cod sursa (job #281941) | Cod sursa (job #2208695) | Cod sursa (job #2243436) | Cod sursa (job #2202074)
#include <bits/stdc++.h>
#define N 1010
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
int n, m, x, y, c, C[N][N], dad[N], flow, maxflow;
vector <int> v[N], nxt;
bool viz[N];
queue <int> q;
bool bfs(){
memset(viz, 0, sizeof viz);
nxt.clear();
while(q.size())
q.pop();
viz[1] = 1;
q.push(1);
bool flag = 1;
while(q.size() && flag){
x = q.front();
q.pop();
if(x == n)
continue;
for(auto i : v[x])
if(!viz[i] && C[x][i] > 0){
viz[i] = 1;
dad[i] = x;
q.push(i);
if(i == n)
nxt.push_back(x);
}
}
return viz[n];
}
int main(){
in >> n >> m;
while(m--){
in >> x >> y >> c;
v[x].push_back(y);
v[y].push_back(x);
C[x][y] = c;
}
while(bfs()){
for(auto it : nxt){
flow = 123456;
for(int to = n, from = it; from != 0; to = from, from = dad[from])
flow = min(flow, C[from][to]);
for(int to = n, from = it; from != 0; to = from, from = dad[from]){
C[from][to] -= flow;
C[to][from] += flow;
}
maxflow += flow;
}
}
out << maxflow;
return 0;
}