Pagini recente » Cod sursa (job #2259450) | Cod sursa (job #1646309) | Cod sursa (job #3185443) | Cod sursa (job #286379) | Cod sursa (job #2603714)
#include <bits/stdc++.h>
#define DAU ios::sync_with_stdio(false); fin.tie(0); fout.tie(0);
#define PLEC fin.close(); fout.close(); return 0;
using namespace std;
const string problem("maxflow");
ifstream fin(problem + ".in");
ofstream fout(problem + ".out");
using VB = vector<bool>;
using VI = vector<int>;
using VVI = vector<VI>;
VVI g, cap;
VI dad;
VB viz;
queue<int> q;
int n, m, x, y, c, nod, flowmax, flowcurr;
inline bool BFS() {
fill(viz.begin(), viz.end(), false);
viz[1] = true;
q.emplace(1);
while (!q.empty()) {
x = q.front();
q.pop();
if (x != n)
for (const int& y : g[x])
if (!viz[y] && cap[x][y])
viz[y] = true, dad[y] = x, q.emplace(y);
}
return viz[n];
}
int Edmonds_Karp() {
dad = VI(n + 1);
viz = VB(n + 1);
while (BFS())
for (const int& nod : g[n])
if (viz[nod] && cap[nod][n]) {
dad[n] = nod;
flowcurr = 1e9;
x = n;
while (x != 1) {
flowcurr = min(flowcurr, cap[dad[x]][x]);
x = dad[x];
}
if (flowcurr) {
x = n;
while (x != 1) {
cap[dad[x]][x] -= flowcurr;
cap[x][dad[x]] += flowcurr;
x = dad[x];
}
flowmax += flowcurr;
}
}
return flowmax;
}
int main() {
DAU
fin >> n >> m;
g = VVI(n + 1);
cap = VVI(n + 1, VI(n + 1));
for (int i = 1; i <= m; ++i) {
fin >> x >> y >> c;
g[x].emplace_back(y);
g[y].emplace_back(x);
cap[x][y] = c;
}
fout << Edmonds_Karp() << '\n';
PLEC
}