Pagini recente » Atasamentele paginii Clasament becreative23 | Cod sursa (job #1656164) | Cod sursa (job #596660) | Atasamentele paginii gimnaziu_1 | Cod sursa (job #1656195)
#include <cstdio>
#include <vector>
#include <queue>
#include <bitset>
#include <algorithm>
using namespace std;
const int NMAX = 1002;
int pumped[NMAX][NMAX], capacity[NMAX][NMAX], path[NMAX], N;
vector<int> graph[NMAX], fathers_of_N;
bitset<NMAX> check;
queue<int> Q;
void BFS () {
check.reset();
Q.push (1);
while (!Q.empty ()) {
int node;
node = Q.front ();
check[node] = true;
Q.pop ();
if (node == N)
continue;
for (auto i : graph[node])
if (!check[i] && capacity[node][i] - pumped[node][i] > 0) {
path[i] = node;
Q.push(i);
}
}
}
int main () {
freopen ("maxflow.in", "r", stdin);
freopen ("maxflow.out", "w", stdout);
int M, ans = 0;
scanf ("%d%d", &N, &M);
while (M--) {
int a, b, f;
scanf ("%d%d%d", &a, &b, &f);
graph[a].push_back(b);
capacity[a][b] = f;
if (b == N) {
fathers_of_N.push_back(a);
}
}
bool stop;
while (true) {
BFS();
stop = true;
if (check[N]) {
for (auto i : fathers_of_N) {
int _min = 2e9;
int node = N;
while (node != 1) {
_min = min (_min, capacity[path[node]][node] - pumped[path[node]][node]);
node = path[node];
}
ans += _min;
node = N;
while (node != 1) {
pumped[path[node]][node] += _min;
pumped[node][path[node]] -= _min;
node = path[node];
}
stop = false;
}
}
if (stop)
break;
}
printf ("%d ", ans);
}