Cod sursa(job #2255764)

Utilizator SqueekDanielTodasca Daniel SqueekDaniel Data 7 octombrie 2018 16:07:59
Problema Flux maxim de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 3.23 kb
#include <bits/stdc++.h>

#define mp   std::make_pair
#define inf  (int)(1e9)
#define MaxN 355
#define NNodes 335
#define Dist first
#define Src  second

std::ifstream InFile("fmcm.in");
std::ofstream OutFile("fmcm.out");

std::queue <int> Queue;
std::priority_queue <std::pair <int, int>, std::vector <std::pair <int, int>>, std::greater <std::pair <int, int>>> PQueue;
int M, S, D;

    bool InQueue[NNodes];
    int Capacity[NNodes][NNodes],
        Cost[NNodes][NNodes];
    int BFDist[NNodes], Dist[NNodes];
    int Parent[NNodes];

    struct GraphNode {
        std::list <int> ADC;
    }   Node[NNodes];

    int N;
    int MinCost;
    inline void AddEdge(int Src, int Dest, int xCapacity, int xCost) {
        Capacity[Src][Dest] = xCapacity;
        Cost[Src][Dest] = xCost;
        Cost[Dest][Src] = -xCost;
        Node[Src].ADC.push_back(Dest);
        Node[Dest].ADC.push_back(Src);
    }

    void BellmanFord() {
        for (int i=0; i<N; ++i)
            BFDist[i] = inf;

        Queue.push(S);
        InQueue[S] = 1;
        BFDist[S] = 0;

        int Front;
        while(!Queue.empty()) {
            Front = Queue.front();
            Queue.pop();

            InQueue[Front] = 0;
            for (auto Vecin:Node[Front].ADC)
                if (Capacity[Front][Vecin] && BFDist[Vecin] > BFDist[Front] + Cost[Front][Vecin]) {
                    BFDist[Vecin] = BFDist[Front] + Cost[Front][Vecin];
                    if (!InQueue[Vecin])
                        InQueue[Vecin] = 1,
                        Queue.push(Vecin);
            }
        }
    }

    void RenewEdges() {
        for (int i=0, j; i<N; ++i)
            for (auto Vecin:Node[i+1].ADC)
                Cost[i+1][Vecin] += BFDist[i+1] - BFDist[Vecin];
    }

    inline bool Dijkstra() {
        for (int i=0; i<N; ++i)
            Dist[i+1] = inf;
        Dist[S] = 0;
        PQueue.push(mp(0, S));

        std::pair <int, int> Top;
        while(!PQueue.empty()) {
            Top = PQueue.top();
            PQueue.pop();

            if (Dist[Top.Src] < Top.Dist) continue;

            for (auto Vecin:Node[Top.Src].ADC)
                if (Capacity[Top.Src][Vecin] && Dist[Vecin] > Dist[Top.Src] + Cost[Top.Src][Vecin]) {
                    Dist[Vecin] = Dist[Top.Src] + Cost[Top.Src][Vecin];
                    Parent[Vecin] = Top.Src;

                    PQueue.push(mp(Dist[Vecin], Vecin));
                 }
        }

        if(Dist[D] == inf) return false;
        int Min = inf, x = D;
        while(x != S)
            Min = std::min(Min, Capacity[Parent[x]][x]),
            x = Parent[x];

        x = D;
        MinCost += Min * (Dist[D] + BFDist[D]);
        while(x != S)
            Capacity[x][Parent[x]] += Min,
            Capacity[Parent[x]][x] -= Min,
            x = Parent[x];
        return true;
    }


void Citire() {
    InFile >> N >> M >> S >> D;
    for (int i=0, x, y, cap, cost; i<M; ++i)
        InFile >> x >> y >> cap >> cost,
        AddEdge(x, y, cap, cost);
}

void Rezolvare() {
    BellmanFord();
    RenewEdges();

    while(Dijkstra());
    OutFile << MinCost << '\n';
}

int main()
{
    Citire();
    Rezolvare();

    return 0;
}