Pagini recente » Cod sursa (job #873145) | Cod sursa (job #1453956) | Cod sursa (job #269766) | Cod sursa (job #1277606) | Cod sursa (job #2308137)
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
const int nMax = 1000, oo = 2000000000;
vector<int> g[nMax + 5];
int n, m, c[nMax + 5][nMax + 5], f[nMax + 5][nMax + 5], t[nMax + 5], viz[nMax + 5], cd[nMax + 5];
int BF() {
cd[0] = cd[1] = 1;
memset(viz, 0, sizeof(viz));
viz[1] = 1;
for (int i = 1; i <= cd[0]; i++) {
int nod = cd[i];
if (nod == n)
continue;
for (auto j : g[nod]) {
if (c[nod][j] == f[nod][j] || viz[j])
continue;
viz[j] = 1;
cd[++cd[0]] = j;
t[j] = nod;
}
}
return viz[n];
}
int main() {
fin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y, z;
fin >> x >> y >> z;
c[x][y] += z;
g[x].push_back(y);
g[y].push_back(x);
}
int flow, fmin;
for (flow = 0; BF(); ) {
for (auto i : g[n]) {
if (f[i][n] == c[i][n] || viz[i] == 0)
continue;
t[n] = i;
fmin = oo;
for (int j = n; j != 1; j = t[j])
fmin = min(fmin, c[t[j]][j] - f[t[j]][j]);
if (fmin == 0)
continue;
for (int j = n; j != 1; j = t[j]) {
f[t[j]][j] +=fmin;
f[j][t[j]] -= fmin;
}
flow += fmin;
}
}
fout << flow <<'\n';
return 0;
}