Cod sursa(job #460733)

Utilizator BitOneSAlexandru BitOne Data 3 iunie 2010 18:15:03
Problema Flux maxim de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.84 kb
#include <queue>
#include <bitset>
#include <cstdlib>
#include <fstream>
#define Nmax 411
#define oo 99999

/*
 *
 */
using namespace std;
typedef pair< int, int > pr;
int N, source, sink;
int d[Nmax], f[Nmax];
int C[Nmax][Nmax], F[Nmax][Nmax];
bitset< Nmax > inH;
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, y, c;
    fill( d+1, d+N+1, oo );
    f[source]=d[source]=0;
    inH.flip(source);
    for( pQ.push(source); !pQ.empty(); )
    {
        x=pQ.top(); pQ.pop();
        inH.flip(x);
        for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
        {
            y=it->first, c=it->second;
            if( source == y )
                continue;
            if( C[x][y] > F[x][y] && d[y] > d[x]+c )
            {
                f[y]=x;
                d[y]=d[x]+c;
                if( false == inH.test(y) )
                {
                    pQ.push(y);
                    inH.flip(y);
                }
            }
        }
    }
    inH&=0;
    return oo != d[sink];
}
int main( void )
{
    int M, x, y, c, s;
    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( x, -c ) );
    }
    for( s=0; MinPath(); s+=c*d[sink] )
    {
        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;
        }
    }
    ofstream out( "fmcm.out" );
    out<<s<<'\n';
    return EXIT_SUCCESS;
}