Pagini recente » Cod sursa (job #69244) | Cod sursa (job #558315) | Cod sursa (job #832469) | Cod sursa (job #1879538) | Cod sursa (job #2427933)
//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>
#include <chrono>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-6;
inline long long lgput(long long a , long long b , long long mod) {
long long ret = 1;
while( b ){
if(b & 1) ret = (ret * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return (ret%mod);
}
inline long long inv(long long x , long long MOD) {
return lgput(x, MOD - 2, MOD);
}
inline bool exist (double nr){
return (nr < -eps) || (nr > eps);
}
long long big_rand(){
return rand () * RAND_MAX + rand();
}
//-----------------------------------------------------------------
//#include <iostream>
#include <fstream>
ifstream cin ("fmcm.in");ofstream cout ("fmcm.out");
int n , S , D;
vector < int > gr[355];
int cap[355][355];
int flow[355][355];
int cost[355][355];
int dad[355];
int dp[355];
int used[355];
queue < int > q;
bool bf(){
for (int i=1; i<=n; i++){
dad[i] = 0;
dp[i] = 1e9;
}
bool ok = 0;
q.push(S);
dp[S] = 0;
dad[S] = -1;
while (!q.empty()){
int now = q.front();
//cerr<<now<<'\n';
q.pop();
used[now] = 0;
if (now == D){
ok = 1;
continue;
}
for (auto &x : gr[now]){
if (flow[now][x] < cap[now][x] && dp[x] > dp[now] + cost[now][x]){
dad[x] = now;
dp[x] = dp[now] + cost[now][x];
if (!used[x]){
q.push(x);
used[x] = 1;
}
}
}
}
return ok;
}
int main() {
//freopen("input", "r", stdin);freopen("output", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout << setprecision(10) << fixed;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
srand(time(nullptr));
int m;
cin>>n>>m>>S>>D;
for (int i=1; i<=m; i++){
int a , b , c , z;
cin>>a>>b>>c>>z;
gr[a].push_back(b);
gr[b].push_back(a);
cap[a][b] += c;
cost[a][b] += z;
cost[b][a] -= z;
}
int ans = 0;
while (bf()){
int MIN = 1e9;
int now = D;
while (now != S){
MIN = min(MIN , cap[dad[now]][now] - flow[dad[now]][now]);
now = dad[now];
}
now = n;
while (now != S){
ans += MIN * cost[dad[now]][now];
flow[dad[now]][now] += MIN;
flow[now][dad[now]] -= MIN;
now = dad[now];
}
}
cout<<ans;
return 0;
}