Cod sursa(job #1408128)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 29 martie 2015 20:06:14
Problema Flux maxim de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 3.49 kb
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 350 + 1;
const int Mmax = 12500 + 1;
const int NIL = -1;
const int INF = numeric_limits<int>::max();

struct NODE
{
    int nod;
    int dist;

    bool operator < ( const NODE &N ) const
    {
        return dist > N.dist;
    }
};

struct Edge
{
    int vecin;

    int urm;
};

Edge Graph[2 * Mmax];
int head[Nmax];

priority_queue <NODE> MinHeap;

int Cost[Nmax + 1][Nmax + 1];
int C[Nmax + 1][Nmax + 1];
int F[Nmax + 1][Nmax + 1];
int tata[Nmax + 1], dist[Nmax + 1], in_q[Nmax + 1];
queue<int> Q;


int N, M;
int countEdges;

void addEdge(int x, int y, int cap, int cost)
{
    Graph[countEdges] = {y, head[x]};
    head[x] = countEdges++;

    Graph[countEdges] = {x, head[y]};
    head[y] = countEdges++;

    C[x][y] = cap;
    Cost[x][y] = +cost;
    Cost[y][x] = -cost;
}

void BellmanFord( int S, int D )
{
    for ( int i = 1; i <= N; ++i )
    {
        dist[i] = INF;
    }

    dist[S] = 0;
    Q.push(S);
    in_q[S] = 1;

    while ( Q.size() )
    {
        int nod = Q.front();
        Q.pop();
        in_q[nod] = 0;

        for (int p = head[nod]; p != NIL; p = Graph[p].urm)
        {
            int x = Graph[p].vecin;

            if ( dist[x] > dist[nod] + Cost[nod][x] && C[nod][x] > F[nod][x] )
            {
                dist[x] = dist[nod] + Cost[nod][x];

                if ( !in_q[x] )
                {
                    in_q[x] = 1;
                    Q.push(x);
                }
            }
        }
    }
}

void updateCosts()
{
    for ( int i = 1; i <= N; ++i )
    {
        if (dist[i] != INF)
        {
           for (int p = head[i]; p != NIL; p = Graph[p].urm)
           {
               int x = Graph[p].vecin;

                if (dist[x] != INF)
                    Cost[i][x] += (dist[i] - dist[x]);
           }
        }
    }
}

int Dijkstra(int S, int T)
{
    updateCosts();

    for ( int i = 1; i <= N; ++i )
    {
        dist[i] = INF;
        tata[i] = 0;
    }

    dist[S] = 0;
    MinHeap.push( {S, 0} );

    while ( MinHeap.size() )
    {
        int nod = MinHeap.top().nod;
        int ddd = MinHeap.top().dist;

        MinHeap.pop();

        if ( dist[nod] != ddd) continue;

        for (int p = head[nod]; p != NIL; p = Graph[p].urm)
        {
            int x = Graph[p].vecin;

            if ( dist[x] > dist[nod] + Cost[nod][x] && C[nod][x] > F[nod][x] )
            {
                dist[x] = dist[nod] + Cost[nod][x];
                tata[x] = nod;
                MinHeap.push( {x, dist[x]} );
            }
        }
    }

    return (dist[T] != INF);
}

long long Edmonds_Karp( int S, int D )
{
    long long costFlow = 0;
    long long flow = 0;
    int fmin, distD = dist[D];

    while ( Dijkstra( S, D ) )
    {
        fmin = INF;

        for ( int nod = D; nod != S; nod = tata[nod] )
                fmin = min( fmin, C[ tata[nod] ][nod] - F[ tata[nod] ][nod] );

        for ( int nod = D; nod != S; nod = tata[nod] )
        {
            F[ tata[nod] ][nod] += fmin;
            F[nod][ tata[nod] ] -= fmin;
        }

        flow += fmin;
        distD += dist[D];
        costFlow += fmin * distD;
    }

    return costFlow;
}

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

    int S, D;

    f >> N >> M >> S >> D;

    for (int i = 1; i <= N; ++i)
        head[i] = NIL;

    for ( int i = 1, a, b, c, d; i <= M; ++i )
    {
        f >> a >> b >> c >> d;

        addEdge(a, b, c, d);
    }

    BellmanFord( S, D );
    g << Edmonds_Karp( S, D ) << "\n";

    return 0;
}