Cod sursa(job #2430007)

Utilizator Alex18maiAlex Enache Alex18mai Data 12 iunie 2019 13:01:54
Problema Flux maxim de cost minim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.2 kb
//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>

using namespace std;

//#include <iostream>
#include <fstream>
ifstream cin ("fmcm.in");ofstream cout ("fmcm.out");

const int inf = 1e9;
const int NMAX = 355;

int n , m , S , D;

vector < int > gr[NMAX];
int cap[NMAX][NMAX];
int cost[NMAX][NMAX];
int DP[NMAX];
int used[NMAX];

queue < int > q;

void norm(){
    for (auto &x : DP){
        x = inf;
    }
    q.push(S);
    DP[S] = 0;
    while (!q.empty()){
        int now = q.front();
        q.pop();
        used[now] = 0;
        for (auto &x : gr[now]){
            if (cap[now][x] && DP[x] > DP[now] + cost[now][x]){
                DP[x] = DP[now] + cost[now][x];
                if (!used[x]){
                    used[x] = 1;
                    q.push(x);
                }
            }
        }
    }
    for (int i=1; i<=n; i++){
        for (int j=1; j<=n; j++){
            cost[i][j] += DP[i] - DP[j];
        }
    }
}

int flow[NMAX][NMAX];
int dad[NMAX];
int dp[NMAX];

class cmp{
public:
    bool operator () (pair < int , int > a , pair < int , int > b){
        return a.second > b.second;
    }
};

priority_queue < pair < int , int > , vector < pair < int , int > > , cmp > Q;



int flux(){
    for (auto &x : dp){
        x = inf;
    }
    dp[S] = 0;
    Q.push({S , 0});
    while (!Q.empty()){
        pair < int , int > now = Q.top();
        Q.pop();
        //cerr<<now.first<<'\n';
        if (now.second != dp[now.first]){
            continue;
        }
        for (auto &x : gr[now.first]){
            if (cap[now.first][x] > flow[now.first][x] && dp[x] > dp[now.first] + cost[now.first][x]){
                dp[x] = dp[now.first] + cost[now.first][x];
                dad[x] = now.first;
                Q.push({x , dp[x]});
            }
        }
    }
    if (dp[D] != inf){
        return 1;
    }
    return 0;
}

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((long long)(new char));
    srand(time(nullptr));

    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;
    }

    norm();

    int ans = 0;

    while (flux()){
        //cerr<<"stop"<<'\n';
        int now = D;
        int MIN = inf;
        while (now != S){
            MIN = min(MIN , cap[dad[now]][now] - flow[dad[now]][now]);
            now = dad[now];
        }
        now = D;
        while (now != S){
            flow[dad[now]][now] += MIN;
            flow[now][dad[now]] -= MIN;
            ans += MIN * (cost[dad[now]][now] - DP[dad[now]] + DP[now]);
            now = dad[now];
        }
    }

    cout<<ans;

    return 0;
}