Pagini recente » Cod sursa (job #986592) | Cod sursa (job #1740148) | Istoria paginii runda/icrisop | Cod sursa (job #1290385) | Cod sursa (job #2694466)
//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];
vector <int> gr[maxn];
//FUNCTIONS
int bfs(vector <int>& dad) {
for (auto& x : dad) {
x = -1;
}
dad[1] = -2;
queue <pair <int, int>> q;
q.push({ 1, inf });
while (!q.empty()) {
int nod = q.front().first;
int flow = q.front().second;
q.pop();
for (auto& x : gr[nod]) {
if (dad[x] == -1 && cap[nod][x]) {
dad[x] = nod;
int new_flow = min(flow, cap[nod][x]);
if (x == n)
return new_flow;
q.push({ x, new_flow });
}
}
}
return 0;
}
//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 flow = 0;
vector <int> dad(n + 1);
int new_flow;
while (new_flow = bfs(dad)) {
flow += new_flow;
int cur = n;
while (cur != 1) {
int prev = dad[cur];
cap[prev][cur] -= new_flow;
cap[cur][prev] += new_flow;
cur = prev;
}
}
cout << flow << '\n';
return 0;
}