Cod sursa(job #1380011)

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

using namespace std;

#define N 355
#define M 12505
#define INF 2147483647


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

int n, m, s, d;
int c[N][N], f[N][N], cost[N][N];
int drum[N];
int total, totalcost = 0;

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

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

int h[N], poz[N], nh = 0;
int t[N];
inline 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;
}

inline void urca(int i)
{
    while(i >= 2 && drum[h[i]] < drum[h[i >> 1]])
    {
        schimba(i, i >> 1);
        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);
    }
}

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

inline void sterge(int i)
{
    schimba(i, nh);
    nh--;
    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;
        t[i] = 0;
        poz[i] = 0;
    }
    drum[s] = 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])
            {
                t[y] = x;
                drum[y] = drum[x] + cost[x][y];
                if(!poz[y])
                    adauga(y);
                else
                    urca(poz[y]);
            }
        }
    }

    total += drum[d];
    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;
                }
            }
        }
    }

    total = 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();

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

        totalcost += minim * total;
    }

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