Cod sursa(job #319398)

Utilizator syntax_errorGustav Matula syntax_error Data 31 mai 2009 17:53:07
Problema Flux maxim Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.59 kb
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>

using namespace std;
typedef vector< int > :: iterator vit;

const int inf = 1000000000;

#define MAXN 1005
#define MAXM 2005

int N, M;
int V[ MAXN ];
int dad[ MAXN ];
int Net[ MAXN ][ MAXN ];
vector< int > G[ MAXN ];

int cookie;

inline int fixflow() {
   int augment = inf;
   for( int son = N; son > 0; son = dad[ son ] ) {
      augment = min( augment, Net[ dad[ son ] ][ son ] );
   }
   for( int son = N; son > 0; son = dad[ son ] ) {
      Net[ dad[ son ] ][ son ] -= augment;
      Net[ son ][ dad[ son ] ] += augment;
   }
   return augment;
}

queue< int > Q;

inline int BFS() {
   memset( dad, -1, sizeof dad );
   
   while( !Q.empty() ) Q.pop(); Q.push( 1 ); 
   V[1] = cookie; 
   
   while( !Q.empty() ) {
      int C = Q.front(); Q.pop();
      
      if( C == N ) return fixflow();
      
      for( vit i = G[C].begin(); i != G[C].end(); ++i ) {
         if( V[*i] == cookie ) continue;
         if( Net[C][*i] <= 0 ) continue;
         
         dad[ *i ] = C;
         V[*i] = cookie; Q.push( *i );
      }
   }
   
   return 0;
}

inline int maxflow() {
   memset( V, 0, sizeof V );
   int ret, flow; cookie = 1; 
   for( ret = 0; ( flow = BFS() ) != 0; ret += flow, ++cookie );
   return ret;
}

int main( void )
{
   freopen( "maxflow.in", "r", stdin );
   freopen( "maxflow.out", "w", stdout );
   
   scanf( "%d%d", &N, &M );
   for( int i = 0; i < M; ++i ) {
      int A, B, C; scanf( "%d%d%d", &A, &B, &C );
      
      Net[ A ][ B ] = C;
      G[ A ].push_back( B );
   }
   
   printf( "%d\n", maxflow() );
   
   return 0;
}