Pagini recente » Cod sursa (job #485748) | Istoria paginii runda/adasd | Cod sursa (job #1683031) | Cod sursa (job #1293119) | Cod sursa (job #1881874)
#include <stdio.h>
#include <vector>
#include <queue>
#include <string.h>
#include <stdlib.h>
#define N 1005
using namespace std;
int n, m, *t, C[N][N], f[N], nr;
long long flow;
vector<int> G[N];
queue<int> Q;
bool BFS(int s)
{
int i, x;
Q.push(s);
memset(t, 0, (n + 1) * sizeof(int));
while (!Q.empty()) {
x = Q.front();
Q.pop();
for (i = 0; i < G[x].size(); i++)
if (!t[G[x][i]] && C[x][G[x][i]]) {
t[G[x][i]] = x;
Q.push(G[x][i]);
}
}
return t[n] != 0;
}
int main()
{
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
scanf("%i%i", &n, &m);
t = (int *) malloc((n + 1) * sizeof(int));
int i, j, x, y, z;
for (i = 1; i <= m; i++) {
scanf("%i%i%i", &x, &y, &z);
G[x].push_back(y);
G[y].push_back(x);
C[x][y] = z;
if (y == n) f[++nr] = x;
}
flow = 0;
while (BFS(1)) {
for (i = 1; i <= nr; i++)
if (C[f[i]][n]) {
z = C[f[i]][n];
for (j = f[i]; j > 1; j = t[j])
z = min(z, C[t[j]][j]);
flow += z;
for (j = f[i]; j > 1; j = t[j]) {
C[t[j]][j] -= z;
C[j][t[j]] += z;
}
C[f[i]][n] -= z;
C[n][f[i]] += z;
}
}
printf("%lld", flow);
fclose(stdin);
fclose(stdout);
return 0;
}