Pagini recente » Cod sursa (job #3227103) | Cod sursa (job #79476) | Cod sursa (job #1311886) | Cod sursa (job #2699254) | Cod sursa (job #2210346)
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, m, c[N][N], pr[N], maxflow;
vector <int> v[N];
bool viz[N];
queue <int> Q;
bool bfs(){
while (!Q.empty()) Q.pop();
for (int i=1; i<=n; i++) viz[i] = 0;
viz[1] = 1;
Q.push(1);
while (!Q.empty()){
int nod = Q.front();
Q.pop();
if (nod == n) break;
for (auto it: v[nod]){
if (viz[it] || !c[nod][it]) continue;
pr[it] = nod;
viz[it] = 1;
Q.push(it);
}
}
return viz[n];
}
//Edmond Karp
int main(){
ifstream cin ("maxflow.in");
ofstream cout ("maxflow.out");
cin >> n >> m;
for (int i=1, x, y, z; i<=m; i++){
cin >> x >> y >> z;
v[x].push_back(y);
v[y].push_back(x);
c[x][y] += z;
}
while (bfs()){
int flow = 1e9;
for (int nod = n; nod != 1; nod = pr[nod])
flow = min(flow, c[pr[nod]][nod]);
for (int nod = n; nod != 1; nod = pr[nod]){
c[pr[nod]][nod] -= flow;
c[nod][pr[nod]] += flow;
}
maxflow += flow;
}
cout << maxflow;
return 0;
}