Pagini recente » Rating Piciu Florin Alexandru (Piciu99) | Cod sursa (job #1633584) | Cod sursa (job #2612890) | Cod sursa (job #2334931) | Cod sursa (job #2736282)
#include <bits/stdc++.h>
using namespace std;
void DAU(const string& task = "") {
if (!task.empty())
freopen((task + ".in").c_str(), "r", stdin),
freopen((task + ".out").c_str(), "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void PLEC() {
exit(0);
}
const int INF(1e9);
using VI = vector<int>;
using VVI = vector<VI>;
using VB = vector<bool>;
class Edmonds_Karp {
public:
Edmonds_Karp(const int& _n = 0) : n(_n), g(_n + 1), dad(_n + 1) {
cap = VVI(n + 1, VI(n + 1));
}
inline void AddEdge(const int& x, const int& y, const int& c) {
g[x].emplace_back(y);
g[y].emplace_back(x);
cap[x][y] = c;
}
inline int MaxFlow() {
int x, flow, flowmax(0);
while (BFS()) {
for (const int& nod : g[n])
if (cap[nod][n]) {
flow = INF;
dad[n] = nod;
x = n;
while (x != 1) {
flow = min(flow, cap[dad[x]][x]);
x = dad[x];
}
if (flow == INF)
continue;
x = n;
while (x != 1) {
cap[dad[x]][x] -= flow;
cap[x][dad[x]] += flow;
x = dad[x];
}
flowmax += flow;
}
}
return flowmax;
}
private:
inline bool BFS() {
queue<int> q;
q.emplace(1);
fill(dad.begin(), dad.end(), 0);
dad[1] = 1;
while (!q.empty()) {
int x = q.front();
q.pop();
if (x == n)
continue;
for (const int& y : g[x])
if (!dad[y] && cap[x][y]) {
dad[y] = x;
q.emplace(y);
}
}
return dad[n];
}
int n;
VVI g, cap;
VI dad;
};
int n, m, x, y, w;
signed main() {
DAU("maxflow");
cin >> n >> m;
Edmonds_Karp graph(n);
while (m--) {
cin >> x >> y >> w;
graph.AddEdge(x, y, w);
}
cout << graph.MaxFlow();
PLEC();
}