Pagini recente » Cod sursa (job #1902589) | Cod sursa (job #2074820) | Cod sursa (job #2760798) | Cod sursa (job #2090403) | Cod sursa (job #2325036)
#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
const int MAX_N = 1e3;
const int INF = 2e9;
std::vector<int> G[2 + MAX_N];
int r[2 + MAX_N][2 + MAX_N];
int parent[2 + MAX_N];
bool visited[2 + MAX_N];
bool bfs(int n, int u) {
std::queue<int> q;
q.push(u);
visited[u] = true;
while (!q.empty() && !visited[n]) {
int u = q.front();
q.pop();
for (int v : G[u])
if (!visited[v] && r[u][v] > 0) {
visited[v] = true;
parent[v] = u;
q.push(v);
}
}
return visited[n];
}
int main() {
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int u, v, flow;
scanf("%d%d%d", &u, &v, &flow);
G[u].push_back(v);
G[v].push_back(u);
r[u][v] = flow;
}
int maxFlow = 0;
while (bfs(n, 1)) {
for (int u : G[n])
if (visited[u]) {
parent[n] = u;
int flow = INF;
int v = n;
while (v != 1) {
flow = std::min(flow, r[parent[v]][v]);
v = parent[v];
}
v = n;
while (v != 1) {
r[parent[v]][v] -= flow;
r[v][parent[v]] += flow;
v = parent[v];
}
maxFlow += flow;
}
for (int i = 1; i <= n; i++)
visited[i] = false;
}
printf("%d", maxFlow);
return 0;
}