Pagini recente » Cod sursa (job #341146) | Cod sursa (job #2232114) | Cod sursa (job #1475595) | Cod sursa (job #1305477) | Cod sursa (job #2875488)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int const N(1005), inf(2e9);
int cap[N][N], n, m, dad[N];
bitset<N> viz;
vector<int> g[N];
queue<int> q;
inline void AddEdge(int const& x, int const& y, int const& w) {
g[x].emplace_back(y);
g[y].emplace_back(x);
cap[x][y] = w;
}
inline bool BF(int const& S = 1, int const& D = n) {
viz.reset();
dad[S] = 0;
viz[S] = 1;
q.emplace(S);
while (!q.empty()) {
int x = q.front();
q.pop();
if (x == D)
continue;
for (int const& y : g[x])
if (!viz[y] && cap[x][y] > 0) {
dad[y] = x;
viz[y] = 1;
q.emplace(y);
}
}
return viz[D];
}
inline int MaxFlow(int const& S = 1, int const& D = n) {
int maxf = 0;
while (BF()) {
for (int const& daddy : g[D]) {
if (cap[daddy][D] < 1 || !viz[daddy])
continue;
dad[D] = daddy;
int flow = inf;
for (int x = D; x != S; x = dad[x])
flow = min(flow, cap[dad[x]][x]);
if (flow < 1)
continue;
for (int x = D; x != S; x = dad[x]) {
cap[dad[x]][x] -= flow;
cap[x][dad[x]] += flow;
}
maxf += flow;
}
}
return maxf;
}
int main() {
fin >> n >> m;
while (m--) {
int x, y, w;
fin >> x >> y >> w;
AddEdge(x, y, w);
}
fout << MaxFlow();
return 0;
}