Pagini recente » Cod sursa (job #1837172) | Cod sursa (job #2293060) | Cod sursa (job #1921268) | Cod sursa (job #2496008) | Cod sursa (job #1628647)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
const int MAXN = 1005;
const int INF = 0x3f3f3f3f;
int n, m, flow;
int f[MAXN][MAXN], c[MAXN][MAXN];
vector<int> g[MAXN];
queue<int> q;
bool vis[MAXN];
int dad[MAXN];
bool bfs() {
for (int i = 1; i <= n; ++i) {
vis[i] = false;
}
q.push(1);
vis[1] = true;
for (; !q.empty(); q.pop()) {
int node = q.front();
if (node != n) {
for (const auto i : g[node]) {
if (f[node][i] < c[node][i] && !vis[i]) {
vis[i] = true;
dad[i] = node;
q.push(i);
}
}
}
}
if (vis[n]) {
return true;
}
return false;
}
int main() {
fin >> n >> m;
for (int i = 1; i <= m; ++i) {
int x, y, z;
fin >> x >> y >> z;
g[x].push_back(y);
g[y].push_back(x);
c[x][y] += z;
}
for (; bfs(); ) {
for (const auto i : g[n]) {
int node = i;
if (f[node][n] < c[node][n] && vis[node]) {
int current_flow = INF;
for (int now = n; now != 1; now = dad[now]) {
current_flow = min(current_flow, c[dad[now]][now] - f[dad[now]][now]);
}
if (current_flow > 0) {
for (int now = n; now != 1; now = dad[now]) {
f[dad[now]][now] += current_flow;
f[now][dad[now]] -= current_flow;
}
flow += current_flow;
}
}
}
}
fout << flow << '\n';
fout.close();
return 0;
}