Pagini recente » Cod sursa (job #366482) | Cod sursa (job #462437) | Cod sursa (job #1132193) | Cod sursa (job #1817133) | Cod sursa (job #3152042)
#include <bits/stdc++.h>
using namespace std;
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()
{
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d %d %d ", &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;
}
printf("%d ", total);
return 0;
}