Cod sursa(job #757948)

Utilizator BitOneSAlexandru BitOne Data 13 iunie 2012 20:57:04
Problema Flux maxim de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.87 kb
#include <queue>
#include <vector>
#include <fstream>
#include <cstdlib>

using namespace std;
typedef pair<int, int> pr;

const int oo=1<<29;
const int N_MAX=361;

vector<pr> G[N_MAX];
vector<pr>::iterator it, iend;
priority_queue<pr, vector<pr>, greater<pr> >pQ;
int S;
int C[N_MAX][N_MAX], F[N_MAX][N_MAX];
int f[N_MAX], d[N_MAX];
bool was[N_MAX];

struct cmp {
    bool operator() (const int& x, const int& y) const {return d[x] > d[y];}
};
void BellmanFord(int start, int end, int N)
{
    int x;
    priority_queue<int, vector<int>, cmp> pQ;

    for(x=1; x <= N; ++x)
        d[x]=oo, was[x]=false;
    d[start]=0;
    for(pQ.push(start); !pQ.empty();)
    {
        x=pQ.top(); pQ.pop();
        was[x]=false;
        for(it=G[x].begin(), iend=G[x].end(); it < iend; ++it)
            if(d[it->first] > d[x] + it->second && C[x][it->first] > F[x][it->first])
            {
                d[it->first]=d[x]+it->second;
                if(false == was[it->first])
                {
                    pQ.push(it->first);
                    was[it->first]=true;
                }
            }
    }
    S=d[end];
}
bool findPath(int start, int end, int N)
{
    pr x;
    int i, j, M;

    for(i=1; i <= N; ++i)
        if(oo != d[i])
        {
            for(j=0, M=G[i].size(); j < M; ++j)
                if(oo != d[G[i][j].first])
                    G[i][j].second+=d[i] - d[G[i][j].first];
        }
    for(i=1; i <= N; ++i)
    {
        d[i]=oo;
        f[i]=-1;
        was[i]=false;
    }
    d[start]=0; f[start]=-2;
    for(pQ.push(pr(0, start)); !pQ.empty(); )
    {
        x=pQ.top(); pQ.pop();
        if(true == was[x.second])
            continue;
        was[x.second]=true;
        for(it=G[x.second].begin(), iend=G[x.second].end(); it < iend; ++it)
            if(false == was[it->first] && d[it->first] > it->second + x.first && C[x.second][it->first] > F[x.second][it->first])
            {
                d[it->first]=it->second+x.first;
                f[it->first]=x.second;
                pQ.push(pr(d[it->first], it->first));
            }
    }
    S+=d[end];
    return -1 != f[end] && oo != d[end];
}
inline int _min(int x, int y) {return x <= y ? x : y;}
int main()
{
    long long int sum;
    int N, M, s, e, x, y, c, cMin;
    ifstream in("fmcm.in");
    ofstream out("fmcm.out");

    for(in>>N>>M>>s>>e; M; --M)
    {
        in>>x>>y;
        in>>C[x][y]>>c;
        G[x].push_back(pr(y, c));
        G[y].push_back(pr(x, -c));
    }
    BellmanFord(s, e, N);
    for(sum=0; findPath(s, e, N); sum+=1LL*cMin*S)
    {
        cMin=oo;
        for(x=e; -2 != f[x]; x=f[x])
            cMin=_min(cMin, C[f[x]][x] - F[f[x]][x]);
        for(x=e; -2 != f[x]; x=f[x])
        {
            F[f[x]][x]+=cMin;
            F[x][f[x]]-=cMin;
        }
    }
    out<<sum<<'\n';
}