Cod sursa(job #460715)

Utilizator BitOneSAlexandru BitOne Data 3 iunie 2010 17:44:45
Problema Flux maxim de cost minim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.87 kb
#include <queue>
#include <cstdlib>
#include <fstream>
#define Nmax 361
#define oo 999999

/*
 *
 */
using namespace std;
typedef pair< int, int > pr;
int N, sink, source;
int d[Nmax], f[Nmax];
int C[Nmax][Nmax], F[Nmax][Nmax];
vector< pr > G[Nmax];
vector< pr >::const_iterator it, iend;
class cmp
{
public:
    inline bool operator() ( const int& x, const int& y )
    {
        return d[x] > d[y];
    }
};
priority_queue< int, vector< int >, cmp > pQ;
inline bool MinPath( void )
{
    int x;
    vector< bool > inH( N+1 );
    for( x=1; x <= N; ++x )
        d[x]=oo, f[x]=-1;
    f[source]=d[source]=0;
    for( pQ.push(source); !pQ.empty(); )
    {
        x=pQ.top(); pQ.pop();
        inH[x]=false;
        for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
        {
            if( source == it->first )
                continue;
            if( C[x][it->first] > F[x][it->first] && d[it->first] > d[x]+it->second )
            {
                f[it->first]=x;
                d[it->first]=d[x]+it->second;
                if( false == inH[it->first] )
                {
                    pQ.push(it->first);
                    inH[it->first]=true;
                }
            }
        }
    }
    return oo != d[N];
}
int main( void )
{
    int M, x, y, c, s=0;
    ifstream in( "fmcm.in" );
    for( in>>N>>M>>source>>sink; M; --M )
    {
        in>>x>>y;
        in>>C[x][y]>>c;
        G[x].push_back( pr( y, c ) );
        G[y].push_back( pr( y, -c ) );
    }
    for( s=0; MinPath(); )
    {
        c=oo;
        for( x=sink; f[x]; x=f[x] )
            c=min( c, C[f[x]][x]-F[f[x]][x] );
        for( x=sink; f[x]; x=f[x] )
        {
            F[f[x]][x]+=c;
            F[x][f[x]]-=c;
        }
        s+=c*d[sink];
    }
    ofstream out("fmcm.out" );
    out<<s<<'\n';
    return EXIT_SUCCESS;
}