Cod sursa(job #398987)

Utilizator ssergiussSergiu-Ioan Ungur ssergiuss Data 19 februarie 2010 18:29:38
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.79 kb
#include <algorithm>
#include <queue>
#include <bitset>
#include <vector>
using namespace std;

#define DIM 1<<10
#define INF 0x3f3f3f3f

int N, M, XXX;
int t[ DIM ], cap[ DIM ][ DIM ];
bitset <DIM> f;
queue <int> q;
vector <int> v[ DIM ];

inline int bf() {

    int nod;
    vector <int> :: iterator it;

    f.reset();
    f[ 1 ] = 1;
    for( q.push( 1 ); !q.empty(); q.pop() )
        if( q.front() != N ) {

            nod = q.front();
            for( it = v[ nod ].begin(); it != v[ nod ].end(); ++it )
                if( cap[ nod ][ *it ] > 0 && !f[ *it ] ) {

                    f[ *it ] = 1;
                    t[ *it ] = nod;
                    q.push( *it );
                }
        }

    return f[ N ];
}

int main() {

    freopen( "maxflow.in", "r", stdin );
    freopen( "maxflow.out", "w", stdout );

    int i, x, y, c, nod, fmin;
    vector <int> :: iterator it;

    scanf( "%d %d", &N, &M );
    for( i = 0; i < M; ++i ) {

        scanf( "%d %d %d", &x, &y, &c );
        v[ x ].push_back( y );
        v[ y ].push_back( x );
        cap[ x ][ y ] = c;
    }

    XXX = 0;
    while( bf() )
        for( it = v[ N ].begin(); it != v[ N ].end(); ++it )
            if( cap[ *it ][ N ] > 0 && f[ *it ] ) {

                t[ N ] = *it;
                fmin = INF;
                for( nod = N; t[ nod ]; nod = t[ nod ] )
                    fmin = min( fmin, cap[ t[ nod ] ][ nod ] );
                if( fmin ) {

                    for( nod = N; t[ nod ]; nod = t[ nod ] ) {

                        cap[ t[ nod ] ][ nod ] -= fmin;
                        cap[ nod ][ t[ nod ] ] += fmin;
                    }
                    XXX += fmin;
                }
            }

    printf( "%d", XXX );

    return 0;
}