Pagini recente » Cod sursa (job #1632407) | Cod sursa (job #3122609) | Cod sursa (job #1159041) | Cod sursa (job #890120) | Cod sursa (job #318465)
Cod sursa(job #318465)
#include <stdio.h>
#include <string.h>
#define MAX_N 1024
int n, m, i, p, q, cpct, flow, improve;
int c[MAX_N][MAX_N];
int Q[MAX_N], flux[MAX_N], tata[MAX_N];
void cit() {
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
scanf("%d %d", &n, &m);
for (i = 1; i <= m; i++) {
scanf("%d %d %d", &p, &q, &cpct);
c[p][q] = cpct;
}
}
void bfs() {
int st = 0, dr = 1;
flux[1] = 2147000000;
while (st < dr) {
st++;
for (i = 1; i <= n; i++)
if (c[Q[st]][i] && flux[i] == 0) {
int x = c[Q[st]][i];
if (flux[Q[st]] < c[Q[st]][i])
x = flux[Q[st]];
flux[i] = x;
tata[i] = Q[st];
Q[++dr] = i;
}
}
improve = flux[n];
if (!improve) return;
int x = n;
while (x != 1) {
c[tata[x]][x] -= flux[n];
c[x][tata[x]] += flux[n];
x = tata[x];
}
}
int main() {
cit();
improve = 1;
while (improve) {
Q[1] = 1;
bfs();
flow += improve;
memset(Q, 0, sizeof(Q));
memset(flux, 0, sizeof(flux));
memset(tata, 0, sizeof(tata));
}
printf("%d\n", flow);
return 0;
}