Cod sursa(job #1651979)

Utilizator lacraruraduRadu Matei Lacraru lacraruradu Data 14 martie 2016 12:49:16
Problema Flux maxim de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 3.44 kb
#include <fstream>
#include <queue>

using namespace std;

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

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

int n, m, s, d;
int c[N][N], f[N][N], cost[N][N];
int lst[N], vf[2 * M], urm[2 * M], nvf = 0;
int drum[N];
bool ok[N];
queue <int> q;
int h[N], poz[N], nh;
int t[N];
int factor, rez = 0;

inline void schimba(int i1, int i2)
{
    swap(h[i1], h[i2]);
    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(bun != i)
    {
        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);
    poz[h[nh]] = 0;
    nh--;
    coboara(i);
}

inline void citire()
{
    in >> n >> m >> s >> d;
    while(m--)
    {
        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;
    }
}

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])
            if(drum[x] + cost[x][vf[i]] < drum[vf[i]] && f[x][vf[i]] < c[x][vf[i]])
            {
                drum[vf[i]] = drum[x] + cost[x][vf[i]];
                if(!ok[vf[i]])
                {
                    q.push(vf[i]);
                    ok[vf[i]] = 1;
                }
            }
    }
    factor = drum[d];
}

inline bool dijkstra()
{
    for(int x = 1; x <= n; x++)
        if(drum[x] != INF)
            for(int i = lst[x]; i; i = urm[i])
                if(drum[vf[i]] != INF)
                    cost[x][vf[i]] += drum[x] - drum[vf[i]];
    for(int i = 1; i <= n; i++)
    {
        drum[i] = INF;
        t[i] = 0;
    }
    drum[s] = 0;
    adauga(s);
    while(nh)
    {
        int x = h[1];
        sterge(1);
        for(int i = lst[x]; i; i = urm[i])
            if(drum[x] + cost[x][vf[i]] < drum[vf[i]] && f[x][vf[i]] < c[x][vf[i]])
            {
                drum[vf[i]] = drum[x] + cost[x][vf[i]];
                t[vf[i]] = x;
                if(poz[vf[i]])
                    urca(poz[vf[i]]);
                else
                    adauga(vf[i]);
            }
    }

    factor += drum[d];
    return (drum[d] != INF);
}

inline void fmcm()
{
    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;
        }

        rez += factor * minim;
    }
}

inline void afisare()
{
    out << rez << '\n';
}

int main()
{
    citire();
    bellmanFord();
    fmcm();
    afisare();
    return 0;
}