Pagini recente » Cod sursa (job #1709984) | Cod sursa (job #291356) | Cod sursa (job #1235880) | Istoria paginii utilizator/roberta_buzenchi | Cod sursa (job #2425278)
#include <bits/stdc++.h>
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
int n, m, x, y, t, viz[1010], c[1010][1010], nxt[1010];
queue<int> q;
vector<int> v[1010];
int bfs() {
memset(viz, 0, sizeof viz);
viz[1] = 1;
q.push(1);
while (!q.empty()) {
auto w = q.front();
q.pop();
if (w == n)
continue;
for (auto it : v[w])
if (!viz[it] && c[w][it] > 0) {
viz[it] = 1;
nxt[it] = w;
q.push(it);
}
}
return viz[n];
}
int main() {
in >> n >> m;
for (int i = 1; i <= m; i++) {
in >> x >> y >> t;
c[x][y] = t;
v[x].push_back(y);
v[y].push_back(x);
}
int flow = 0;
while (bfs()) {
for (auto it : v[n]) {
int mn = 2e9;
for (int j = n, i = it; i; j = i, i = nxt[i])
mn = min(mn, c[i][j]);
flow += mn;
for (int j = n, i = it; i; j = i, i = nxt[i]) {
c[i][j] -= mn;
c[j][i] += mn;
}
}
}
out << flow;
return 0;
}