Pagini recente » Cod sursa (job #2036547) | Cod sursa (job #1164104) | Cod sursa (job #482345) | Cod sursa (job #2157468) | Cod sursa (job #2948565)
#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;
//using namespace __gnu_pbds;
mt19937_64 gen(time(0));
uniform_int_distribution <int64_t> rng;
#ifdef LOCAL
ifstream cin("test.in");
ofstream cout("test.out");
#else
ifstream cin("fmcm.in");
ofstream cout("fmcm.out");
#endif
const int N = 20000;
struct Flow {
struct Edge {
int to;
int cap;
int ind;
int cost;
};
int n;
int dist[N + 5], lst[N + 5];
vector <Edge> g[N + 5];
void init(int _n) {
n = _n;
for (int i = 1; i <= n; i++)
g[i].clear();
}
void add_edge(int x, int y, int z, int cost) {
g[x].push_back({ y, z, (int)g[y].size(), cost });
g[y].push_back({ x, 0, (int)g[x].size() - 1, -cost });
}
int bfs(int src, int dst) {
queue <int> q;
for (int i = 1; i <= n; i++)
dist[i] = (int)1e9, lst[i] = -1;
dist[src] = 0;
q.push(src);
while (!q.empty()) {
int node = q.front();
q.pop();
int ind = 0;
for (auto &[son, cap, ind, cost] : g[node]) {
if (cap > 0 && dist[son] > dist[node] + cost) {
dist[son] = dist[node] + cost;
lst[son] = ind;
q.push(son);
}
}
}
if (dist[dst] == (int)1e9)
return (int)-1e9;
return dist[dst];
}
int max_flow(int src, int dst) {
int total_flow = 0, total_cost = 0, cost, flow;
while ((cost = bfs(src, dst)) != (int)-1e9) {
int node = dst;
flow = (int)1e9;
while (node != src) {
int prev_node = g[node][lst[node]].to, ind = g[node][lst[node]].ind;
flow = min(flow, g[prev_node][ind].cap);
node = prev_node;
}
node = dst;
while (node != src) {
int prev_node = g[node][lst[node]].to, ind = g[node][lst[node]].ind;
g[prev_node][ind].cap -= flow;
g[node][lst[node]].cap += flow;
node = prev_node;
}
total_flow += flow;
total_cost += flow * cost;
}
return total_cost;
}
} flow;
void solve(int test) {
ld t1 = clock();
int n, m, src, dst;
cin >> n >> m >> src >> dst;
flow.init(n);
for (int i = 1; i <= m; i++) {
int x, y, z, c;
cin >> x >> y >> z >> c;
flow.add_edge(x, y, z, c);
}
cout << flow.max_flow(src, dst) << "\n";
#ifdef LOCAL
cout << (clock() - t1) / CLOCKS_PER_SEC << "s\n";
#endif
}
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;
}