Pagini recente » Cod sursa (job #828364) | Cod sursa (job #1295997) | Cod sursa (job #2483343) | Cod sursa (job #1633586) | Cod sursa (job #2738549)
#include <bits/stdc++.h>
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
const int inf = (int)1e9 + 7;
const int max_n = (int)1e3 + 5;
int n, m;
int dad[max_n];
int r[max_n][max_n];
bool visited[max_n];
vector<int> g[max_n];
bool bfs(int startNode, int endNode) {
queue<int> q;
q.push(startNode);
for (int i = 1; i <= n; i++) {
visited[i] = false;
dad[i] = -1;
}
visited[startNode] = true;
while ((int)q.size() > 0) {
int u = q.front();
q.pop();
if (u == endNode) {
continue;
}
for (int v : g[u]) {
if (r[u][v] > 0 && !visited[v]) {
visited[v] = true;
q.push(v);
dad[v] = u;
}
}
}
return visited[endNode];
}
int main() {
in >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v, c;
in >> u >> v >> c;
g[u].push_back(v);
g[v].push_back(u);
r[u][v] = c;
}
int flow = 0;
while (bfs(1, n) == true) {
for (int u : g[n]) {
if (!visited[u] || r[u][n] == 0 || u == n) {
continue;
}
int min_flow = inf;
for (int i = n; i != 1; i = dad[i]) {
min_flow = min(min_flow, r[dad[i]][i]);
}
for (int i = n; i != 1; i = dad[i]) {
r[dad[i]][i] -= min_flow;
r[i][dad[i]] += min_flow;
}
flow += min_flow;
}
}
out << flow << "\n";
return 0;
}