Pagini recente » Cod sursa (job #1103367) | Cod sursa (job #2594884) | Cod sursa (job #994883) | Cod sursa (job #876166) | Cod sursa (job #1206793)
#include <fstream>
#include <cstring>
#include <vector>
#define DIM 1010
using namespace std;
int F[DIM][DIM], C[DIM][DIM];
vector<int> L[DIM];
int q[DIM];
int t[DIM];
int v[DIM];
int sol, minim, i, x, n, m, p, u, y, c;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int bfs() {
memset(v, 0, sizeof (v));
p = 1;
u = 1;
q[1] = 1;
v[1] = 1;
while (p<=u) {
x = q[p];
for (int i=0; i<L[x].size(); i++)
if (v[ L[x][i] ] == 0 && C[x][ L[x][i] ] > F[x][ L[x][i] ]) {
u++;
q[u] = L[x][i];
v[ L[x][i] ] = 1;
t[ L[x][i] ] = x;
}
p++;
}
return v[n];
}
int main() {
fin>>n>>m;
for (i=1;i<=m;i++) {
fin>>x>>y>>c;
L[x].push_back(y);
L[y].push_back(x);
C[x][y] = c;
}
while (bfs()) {
// parcurg vecinii x ai destinatiei pentru care C[x][n] > F[x][n]
for (i=0;i<L[n].size(); i++) {
x = L[n][i];
if (C[x][n] > F[x][n] && v[x] == 1) {
minim = C[x][n] - F[x][n];
for (;x!=1;x = t[x]) {
if (minim > C[ t[x] ][x] - F[ t[x] ][x])
minim = C[ t[x] ][x] - F[ t[x] ][x];
}
x = L[n][i];
F[x][n] += minim;
F[n][x] -= minim;
for (;x!=1;x = t[x]) {
F[ t[x] ][x] += minim;
F[ x ][t[x]] -= minim;
}
sol += minim;
}
}
}
fout<<sol;
return 0;
}