Cod sursa(job #2117187)

Utilizator LucaSeriSeritan Luca LucaSeri Data 28 ianuarie 2018 17:34:13
Problema Flux maxim de cost minim Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 2.86 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 355;
const int INF = (1<<30);

int capacity[MAXN][MAXN];
int current_flow[MAXN][MAXN];
int dist[MAXN];
int old[MAXN];
int viz[MAXN];

vector< pair<int, int> > gr[MAXN];
int boss[MAXN];

ifstream f("fmcm.in");
ofstream g("fmcm.out");

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

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

bool dijkstra(int source, int target, int n){
    for(int i = 1; i <= n; ++i){
        dist[i] = INF;
    }
    memset(boss, 0, sizeof boss);
    memset(viz, 0, sizeof viz);
    dist[source] = 0;
    H.push({source, 0});
    while(H.size()){
        int node = H.top().first;
        int cost = H.top().second;
        H.pop();
        if(viz[node]) continue;

        viz[node] = true;

        for(auto x : gr[node]){
            int c = x.second  + old[node] - old[x.first];

            if(c + cost < dist[x.first] && current_flow[node][x.first] < capacity[node][x.first]){
                dist[x.first] = cost + c;
                boss[x.first] = node;
                H.push({x.first, dist[x.first]});
            }
        }
    }

    for(int i = 1; i <= n; ++i){
        dist[i] += old[i] - old[source];
    }

    return viz[target];
}

void bf(int source, int target, int n){
    memset(old, 0x3f, sizeof old);
    memset(viz, 0, sizeof viz);
    old[source] = 0;

    queue< int > Q;

    Q.push(source);
    viz[source] = true;
    while(Q.size()){
        int node = Q.front();
        viz[node] = false;
        Q.pop();
        for(auto x : gr[node]){
            if(old[node] + x.second >= old[x.first]) continue;

            old[x.first] = old[node] + x.second;
            if(viz[x.first]) continue;

            viz[x.first] = true;
            Q.push(x.first);
        }
    }
}
int main(){
    int n, m, source, target;
    f >> n >> m >> source >> target;

    while(m--){
        int a, b, c, d;
        f >> a >> b >> c >> d;
        gr[a].push_back({b, d});
        capacity[a][b] += c;
    }

    bf(source, target, n);
    int ans = 0;
    while(dijkstra(source, target, n)){
        int node = target;
        int minimum_flow = INF;
        while(node != source){
            minimum_flow = min(minimum_flow, capacity[ boss[node] ][node] - current_flow[ boss[node] ][node]);
            node = boss[node];
        }

        node = target;
        while(node != source){
            current_flow[ boss[node] ][node] += minimum_flow;
            current_flow[node][ boss[node] ] -= minimum_flow;
            node = boss[node];
        }

        ans += (dist[target] * minimum_flow);
        memcpy(old, dist, sizeof dist);
    }

    g << ans;
    return 0;
}