Pagini recente » Cod sursa (job #686458) | Cod sursa (job #1270691) | Cod sursa (job #714487) | Cod sursa (job #2690872) | Cod sursa (job #3152041)
#include <bits/stdc++.h>
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
int n, m, x, y, cap, total;
vector<int> adj[1005];
int f[1005][1005];
int c[1005][1005];
void bfs(int node, int n, vector<int> &p) {
queue<int> q;
q.push(node);
while (!q.empty()) {
int nod = q.front();
q.pop();
for (auto it : adj[nod]) {
if (c[nod][it] - f[nod][it] > 0 && p[it] == -1 && it != p[nod]) {
p[it] = nod;
q.push(it);
}
}
}
}
int main()
{
in >> n >> m;
for (int i = 1; i <= m; i++) {
in >> x >> y >> cap;
adj[x].push_back(y);
adj[y].push_back(x);
c[x][y] += cap;
}
while (1) {
vector<int> p(n + 1, -1);
bfs(1, n, p);
if (p[n] == -1)
break;
int rc = 1e9;
int nod = n;
do {
rc = min(rc, c[p[nod]][nod] - f[p[nod]][nod]);
nod = p[nod];
} while (nod != 1);
nod = n;
do {
f[nod][p[nod]] -= rc;
f[p[nod]][nod] += rc;
nod = p[nod];
} while (nod != 1);
total += rc;
}
out << total << '\n';
return 0;
}