Pagini recente » Cod sursa (job #2132642) | Cod sursa (job #818980) | Cod sursa (job #1094679) | Cod sursa (job #175760) | Cod sursa (job #2694472)
//ALEXANDRU MICLEA
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <time.h>
#include <iomanip>
#include <deque>
#include <math.h>
#include <cmath>
#include <assert.h>
#include <stack>
#include <bitset>
#include <random>
#include <chrono>
#include <assert.h>
#include <iostream>
using namespace std;
using ll = long long;
#define fast_cin() ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
//VARIABLES
const int maxn = 1e3 + 5;
int inf = 1e9 + 5;
int n, m;
int cap[maxn][maxn];
int flow[maxn][maxn];
int dad[maxn];
vector <int> gr[maxn];
queue <int> q;
//FUNCTIONS
int bfs() {
for (auto& x : dad) x = 0;
dad[1] = 1;
q.push(1);
int ok = 0;
while (!q.empty()) {
int nod = q.front();
q.pop();
if (nod == n) {
ok = 1;
continue;
}
for (auto& x : gr[nod]) {
if (!dad[x] && cap[nod][x] - flow[nod][x] > 0) {
dad[x] = nod;
q.push(x);
}
}
}
return ok;
}
//MAIN
int main() {
#ifdef INFOARENA
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
#endif
fast_cin();
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b, val;
cin >> a >> b >> val;
gr[a].push_back(b);
gr[b].push_back(a);
cap[a][b] += val;
}
int ans = 0;
while (bfs()) {
for (auto& x : gr[n]) {
if (!dad[x]) continue;
dad[n] = x;
int now = n;
int minn = inf;
while (now != 1) {
minn = min(minn, cap[dad[now]][now] - flow[dad[now]][now]);
now = dad[now];
}
now = n;
while (now != 1) {
flow[dad[now]][now] += minn;
flow[now][dad[now]] -= minn;
now = dad[now];
}
ans += minn;
}
}
cout << ans << '\n';
return 0;
}