Pagini recente » Cod sursa (job #2381420) | Cod sursa (job #362660) | Cod sursa (job #104254) | Cod sursa (job #2929884) | Cod sursa (job #1969327)
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int nmax = 1e3 + 10;
int n, m;
vector < int > g[nmax];
int S, D,flow;
int dad[nmax], f[nmax][nmax], c[nmax][nmax];
void add_edge(int x, int y, int cap) {
g[x].push_back(y);
g[y].push_back(x);
c[x][y] = cap;
}
bool bfs() {
queue < int > q;
memset(dad, -1, sizeof(dad));
q.push(S);
while (q.size()) {
int node = q.front(); q.pop();
for (auto &it: g[node]) {
if (f[node][it] == c[node][it]) continue;
if (dad[it] != -1) continue;
dad[it] = node;
if (it != D) q.push(it);
}
}
return dad[D] != -1;
}
void maxflow() {
S = 1; D = n;
while (bfs()) {
for (auto &it: g[D]) {
if (dad[it] == -1) continue;
dad[D] = it;
int path_capacity = inf;
for (int node = D; node != S; node = dad[node])
path_capacity = min(path_capacity, c[dad[node]][node] - f[dad[node]][node]);
flow += path_capacity;
for (int node = D; node != S; node = dad[node])
f[dad[node]][node] += path_capacity,
f[node][dad[node]] -= path_capacity;
}
}
}
int main() {
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; ++i) {
int x, y, c; scanf("%d %d %d", &x, &y, &c);
add_edge(x, y, c);
}
maxflow();
printf("%d\n", flow);
return 0;
}