Pagini recente » Cod sursa (job #1082464) | Cod sursa (job #848011) | Cod sursa (job #1358560) | Cod sursa (job #1091310) | Cod sursa (job #1993708)
#include <cstdio>
#include <vector>
#include <cstring>
#define INF 1000000000
#define MAXN 1000
#define MAXM 10000
FILE *fin = fopen("maxflow.in", "r"), *fout = fopen("maxflow.out", "w");
struct myc {
int x, y;
myc(int _x, int _y) {
x = _x;
y = _y;
}
};
class maxFlow {
int s, d, k, ans;
int f[MAXM], c[MAXM];
std::vector <myc> g[MAXN];
bool viz[MAXN];
int q[MAXN], from[MAXN], cine[MAXN];
public :
inline void initialize(int n) {
s = k = ans = 0;
d = n;
}
inline void add(int x, int y, int z, int t) {
muchie(x, y, z);
muchie(y, x, t);
}
inline int flux() {
while (bfs()) {
for (auto &x : g[d]) {
if (viz[x.x] && c[1 ^ x.y] > f[1 ^ x.y]) {
from[d] = x.x;
cine[d] = 1 ^ x.y;
go();
}
}
}
return ans;
}
private :
inline void muchie(int x, int y, int z) {
f[k] = 0;
c[k] = z;
g[x].push_back(myc(y, k));
k++;
}
inline void go() {
int y = d, r = INF;
while(y != s) {
r = std::min(r, c[cine[y]] - f[cine[y]]);
y = from[y];
}
y = d;
while(y != s) {
f[cine[y]] += r;
f[1 ^ cine[y]] -= r;
y = from[y];
}
ans += r;
}
inline bool bfs() {
memset(viz, 0, sizeof(bool) * (d + 1));
int st = 0, dr = 1;
viz[s] = 1;
q[0] = s;
while (st < dr && viz[d] == 0) {
int x = q[st++];
for (auto &y : g[x]) {
if (viz[y.x] == 0 && c[y.y] > f[y.y]) {
viz[y.x] = 1;
from[y.x] = x;
cine[y.x] = y.y;
q[dr++] = y.x;
}
}
}
return viz[d];
}
}T;
int main() {
int n, m;
fscanf(fin, "%d%d", &n, &m);
T.initialize(n - 1);
for (int i = 0; i < m; i++) {
int x, y, z;
fscanf(fin, "%d%d%d", &x, &y, &z);
T.add(x - 1, y - 1, z, 0);
}
fprintf(fout, "%d\n", T.flux());
fclose(fin);
fclose(fout);
return 0;
}