Pagini recente » Cod sursa (job #2768186) | Cod sursa (job #538360) | Cod sursa (job #1656597) | Cod sursa (job #1890995) | Cod sursa (job #812100)
Cod sursa(job #812100)
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
using namespace std;
inline int next_int() {
int d;
scanf("%d", &d);
return d;
}
const int V = 1 << 10;
const int E = 1 << 13;
int n, m;
int cap[V][V], Q[V], seen[V], from[V];
int bfs(int s, int d) {
memset(seen, 0, sizeof seen);
memset(from, -1, sizeof from);
int front = 0, back = 0;
Q[back++] = s;
seen[s] = true;
while (back - front > 0) {
int u = Q[front++];
for (int v = 1; v <= n; v++) {
if (seen[v] == false && cap[u][v] > 0) {
Q[back++] = v;
seen[v] = true;
from[v] = u;
}
}
}
int flow = 1 << 30, where = d;
while (from[where] != -1) {
flow = min(flow, cap[from[where]][where]);
where = from[where];
}
where = d;
while (from[where] != -1) {
cap[from[where]][where] -= flow;
cap[where][from[where]] += flow;
where = from[where];
}
return flow == 1 << 30 ? 0 : flow;
}
int main() {
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
n = next_int();
m = next_int();
for (int i = 0; i < m; i++) {
int u = next_int();
int v = next_int();
int c = next_int();
cap[u][v] += c;
}
int flow = 0;
while (int f = bfs(1, n)) {
flow += f;
}
printf("%d\n", flow);
return 0;
}