Cod sursa(job #406255)

Utilizator alexandru92alexandru alexandru92 Data 1 martie 2010 13:00:44
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.67 kb
#include <queue>
#include <vector>
#include <fstream>
#define INF 0x3f3f3f3f


/*
 *
 */
 using namespace std;
 int N, ss, ds;
 int C[1000][1000];
 vector< int > d, father;
 vector< vector< int > > G;
 vector< int >::const_iterator it, iend;
 inline const int& min( const int& x, const int& y )
 {
     if( x < y )
        return x;
     return y;
 }
 int find_path( void )
 {
     int x;
     queue< int > Q;
     d[0]=INF;
     father.assign( N, -1 );
     Q.push( ss );
     while( -1 == father[ds] && !Q.empty() )
     {
         x=Q.front(); Q.pop();
         for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
            if( -1 == father[*it] && C[x][*it] )
            {
                Q.push( *it );
                father[*it]=x;
                d[*it]=min( d[x], C[x][*it] );
            }
     }
     if( -1 == father[ds] )
        return 0;
     father[ss]=-1;
     for( x=ds; -1 != father[x]; x=father[x] )
     {
         C[father[x]][x]-=d[ds];
         if( 0 == C[x][father[x]] )
            G[x].push_back( father[x] );
         C[x][father[x]]+=d[ds];
     }
     return d[ds];
 }
 int Maximum_Flow( void )
 {
     int path_c, s=0;
     for( path_c=find_path(); path_c; s+=path_c, path_c=find_path() );
     return s;
 }
 int main( void )
 {
     int M, x, y;
     ifstream in( "maxflow.in" );
     in>>N>>M;
     G.resize(N);
     d.resize(N);
     father.resize(N);
     for( ; M; --M )
     {
         in>>x>>y;
         --x, --y;
         in>>C[x][y];
         G[x].push_back(y);
     }
     ss=0; ds=N-1;
     //in>>ss>>ds;
     ofstream out( "maxflow.out" );
     out<<Maximum_Flow();
     return 0;
 }