Pagini recente » Cod sursa (job #2756332) | Cod sursa (job #2280713) | Cod sursa (job #1836640) | Cod sursa (job #2943878)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <cstring>
#include <chrono>
#include <cassert>
#include <bitset>
#include <stack>
#include <queue>
#include <iomanip>
#include <random>
#include <string>
#include <complex>
//#include <ext/pb_ds/assoc_container.hpp>
#ifdef _MSC_VER
# include <intrin.h>
# define __builtin_popcount __popcnt
# define __builtin_popcountll __popcnt64
#endif
#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define x first
#define y second
#define ld long double
#define ll long long
#define ull unsigned long long
#define us unsigned short
#define lsb(x) ((x) & (-(x)))
#define pii pair <int, int>
#define pll pair <ll, ll>
using namespace std;
mt19937_64 gen(time(0));
uniform_int_distribution <int64_t> rng;
const int MOD = (int)1e9 + 7;
namespace recurrences {
// binary exponentiation
template <int MOD>
int lgput(int n, int p) {
int ans = 1, x = n;
while (p) {
if (p & 1)
ans = 1LL * ans * x % MOD;
x = 1LL * x * x % MOD;
p >>= 1;
}
return ans;
}
// modular integer class
template <int MOD>
struct Int {
int x;
Int() {
x = 0;
}
Int(int _x) {
if (_x < 0)
_x += MOD;
if (_x >= MOD)
_x -= MOD;
x = _x;
}
friend ostream& operator << (ostream& os, const Int& X) {
os << (false ? X.x - MOD : X.x);
return os;
}
Int operator + (const Int& other) const {
int val = x + other.x;
return (val >= MOD ? val - MOD : val);
}
Int operator += (const Int& other) {
return *this = *this + other;
}
Int operator - (const Int& other) const {
int val = x - other.x;
return (val < 0 ? val + MOD : val);
}
Int operator -= (const Int& other) {
return *this = *this - other;
}
Int operator * (const Int& other) const {
return 1LL * x * other.x % MOD;
}
Int operator *= (const Int& other) {
return *this = *this * other;
}
Int operator / (const Int& other) const {
return 1LL * x * other.inv() % MOD;
}
bool operator == (const Int& other) const {
return x == other.x;
}
bool operator != (const Int& other) const {
return x != other.x;
}
Int pow(int p) const {
return lgput<MOD>(x, p);
}
int inv() const {
return lgput<MOD>(x, MOD - 2);
}
};
};
template <typename T>
T gcd(T a, T b) {
if (!a)
return b;
return gcd<T>(b % a, a);
}
using namespace recurrences;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
struct Flow {
int n, ops;
int cap[1005][1005];
int best[1005], lvl[1005];
vector <int> g[1005];
void init(int _n) {
n = _n;
memset(cap, 0, sizeof(cap));
for (int i = 1; i <= n; i++)
g[i].clear();
}
void add_edge(int x, int y, int z) {
cap[x][y] += z;
g[x].push_back(y);
g[y].push_back(x);
}
bool bfs(int src, int dst) {
memset(lvl, 0, sizeof(lvl));
queue <int> q;
lvl[src] = 1;
q.push(src);
ops = 0;
while (!q.empty()) {
int node = q.front();
q.pop();
for (auto& son : g[node]) {
ops++;
if (cap[node][son] > 0 && !lvl[son]) {
lvl[son] = lvl[node] + 1;
q.push(son);
}
}
}
return lvl[dst] != 0;
}
int dfs(int node, int dst, int value = (int)1e9) {
if (node == dst)
return value;
for (auto& son : g[node]) {
ops++;
if (cap[node][son] <= 0 || lvl[son] != lvl[node] + 1)
continue;
int flow = dfs(son, dst, min(value, cap[node][son]));
if (flow) {
cap[node][son] -= flow;
cap[son][node] += flow;
return flow;
}
}
return 0;
}
int max_flow(int src, int dst) {
int total_flow = 0, flow;
while(bfs(src, dst)) {
do {
flow = dfs(src, dst);
total_flow += flow;
//cout << ops << " " << flow << "\n";
} while (flow);
}
return total_flow;
}
} flow;
int n, m;
void solve(int test) {
//ld t1 = clock();
in >> n >> m;
flow.init(n);
for (; m; m--) {
int x, y, z;
in >> x >> y >> z;
flow.add_edge(x, y, z);
}
out << flow.max_flow(1, n) << "\n";
//out << (clock() - t1) / CLOCKS_PER_SEC << "s\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
srand(time(0));
int T = 1;
//cin >> T;
for (int t = 1; t <= T; t++) {
solve(t);
}
return 0;
}