Cod sursa(job #1379928)

Utilizator lacraruraduRadu Matei Lacraru lacraruradu Data 6 martie 2015 20:29:27
Problema Flux maxim de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 3.57 kb
#include <fstream>
#include <queue>

using namespace std;

#define N 351
#define M 12501
#define INF 2147483647

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

int S, D;
int n, m;
int c[N][N], f[N][N], cost[N][N];

int lst[N], vf[2 * M], urm[2 * M], nvf = 0;

bool ok[N];
int drum[N];
queue<int> q;

int h[N], poz[N], nh = 0;
int t[N];

int costtotal;

void schimba(int i1, int i2)
{
    int aux = h[i1];
    h[i1] = h[i2];
    h[i2] = aux;
    poz[h[i1]] = i1;
    poz[h[i2]] = i2;
}

void urca(int i)
{
    while(i >= 2 && drum[h[i]] < drum[h[(i >> 1)]])
    {
        schimba(i, (i >> 1));
        i = i >> 1;
    }
}

void coboara(int i)
{
    int bun = i, fs = (i << 1), fd = (i << 1) + 1;
    if(fs <= nh && drum[h[fs]] < drum[h[bun]])
        bun = fs;
    if(fd <= nh && drum[h[fd]] < drum[h[bun]])
        bun = fd;
    if(i != bun)
    {
        schimba(i, bun);
        coboara(bun);
    }
}

void adauga(int x)
{
    h[++nh] = x;
    poz[x] = nh;
    urca(nh);
}

void sterge(int i)
{
    poz[h[i]] = 0;
    schimba(i, nh);
    nh--;
    urca(i);
    coboara(i);
}

inline bool dijkstra()
{
    for(int x = 1; x <= n; x++)
        if(drum[x] != INF)
            for(int i = lst[x]; i; i = urm[i])
            {
                int y = vf[i];
                if(drum[y] != INF)
                    cost[x][y] += drum[x] - drum[y];
            }

    for(int i = 1; i <= n; i++)
        drum[i] = INF;
    drum[S] = 0;

    for(int i = 1; i <= n; i++)
    {
        poz[i] = 0;
        t[i] = 0;
    }

    adauga(S);
    while(nh)
    {
        int x = h[1];
        sterge(1);

        for(int i = lst[x]; i; i = urm[i])
        {
            int y = vf[i];

            if(drum[x] + cost[x][y] < drum[y] && c[x][y] > f[x][y])
            {
                drum[y] = drum[x] + cost[x][y];
                t[y] = x;

                if(!poz[y])
                    adauga(y);
                else
                    urca(poz[y]);
            }
        }
    }

    return(drum[D] != INF);
}

inline void bellmanford()
{
    for(int i = 1; i <= n; i++)
        drum[i] = INF;
    drum[S] = 0;
    q.push(S);
    ok[S] = 1;
    while(!q.empty())
    {
        int x = q.front();
        q.pop();
        ok[x] = 0;

        for(int i = lst[x]; i; i = urm[i])
        {
            int y = vf[i];
            if(drum[x] + cost[x][y] < drum[y] && c[x][y] > f[x][y])
            {
                drum[y] = drum[x] + cost[x][y];
                if(!ok[y])
                {
                    q.push(y);
                    ok[y] = 1;
                }
            }
        }
    }

    costtotal = drum[D];
}

int main()
{
    in >> n >> m >> S >> D;

    for(int i = 1; i <= m; i++)
    {
        int x, y;
        in >> x >> y;
        in >> c[x][y] >> cost[x][y];
        cost[y][x] = -cost[x][y];

        vf[++nvf] = y;
        urm[nvf] = lst[x];
        lst[x] = nvf;
        vf[++nvf] = x;
        urm[nvf] = lst[y];
        lst[y] = nvf;
    }

    bellmanford();

    int costminim = 0;

    for(; dijkstra();)
    {
        int minim = INF;

        for(int x = D; x != S; x = t[x])
            if(minim > c[t[x]][x] - f[t[x]][x])
                minim = c[t[x]][x] - f[t[x]][x];

        for(int x = D; x != S; x = t[x])
        {
            f[t[x]][x] += minim;
            f[x][t[x]] -= minim;
        }

        costtotal += drum[D];
        costminim += costtotal * minim;
    }

    out << costminim << '\n';
    return 0;
}