Pagini recente » Cod sursa (job #2365546) | Cod sursa (job #2066347) | Rating Paun Tudor (Peafowl) | Cod sursa (job #907424) | Cod sursa (job #2470446)
//ALEX ENACHE
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <time.h>
#include <iomanip>
#include <deque>
#include <math.h>
#include <assert.h>
#include <stack>
#include <bitset>
#include <random>
using namespace std;
//-----------------------------------------------------------------
#include <fstream>
//ifstream cin("input"); ofstream cout("output");
ifstream cin("maxflow.in"); ofstream cout("maxflow.out");
const int inf = 1e9;
const int MAXN = 1010;
int n, m;
int S, D;
vector < int > gr[MAXN];
int cap[MAXN][MAXN];
int flow[MAXN][MAXN];
int dad[MAXN];
queue < int > q;
bool bfs() {
for (int i = 1; i <= n; i++) {
dad[i] = 0;
}
dad[S] = S;
q.push(S);
int ok = 0;
while (!q.empty()) {
int now = q.front();
q.pop();
if (now == D) {
ok = 1;
continue;
}
for (auto& x : gr[now]) {
if (!dad[x] && cap[now][x] - flow[now][x] > 0) {
dad[x] = now;
q.push(x);
}
}
}
return ok;
}
int main() {
cin >> n >> m;
S = 1; D = n;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
cap[a][b] += c;
gr[a].push_back(b);
gr[b].push_back(a);
}
int ans = 0;
while (bfs()) {
for (auto& x : gr[D]) {
if (!dad[x]) {
continue;
}
dad[D] = x;
int now = D;
int MIN = inf;
while (now != S) {
MIN = min(MIN, cap[dad[now]][now] - flow[dad[now]][now]);
now = dad[now];
}
now = D;
while (now != S) {
flow[dad[now]][now] += MIN;
flow[now][dad[now]] -= MIN;
now = dad[now];
}
ans += MIN;
}
}
cout << ans;
return 0;
}