Cod sursa(job #572261)

Utilizator raica_cristiraica dumitru cristian raica_cristi Data 5 aprilie 2011 10:04:41
Problema Flux maxim Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 2.31 kb
#include<stdio.h>
#include<vector>
#include<queue>
#include<string.h>
#include<ctype.h>

using namespace std;

#define dim 1001
#define pb push_back
#define INF 0x3f3f3f3f
#define buf 2500

int c[dim][dim], f[dim][dim];
int viz[dim] ,t[dim] , n , m ,poz;
vector <int> v[dim];
queue <int> q;
char ch[buf];

void r ( int &x )
{
    x = 0;
    while ( !isdigit ( ch[poz]))
    {
        poz++;
        if ( poz >=buf )
        fread ( ch ,1,  buf-1 , stdin) , poz = 0;
    }
    while ( isdigit ( ch[poz]) )
    {
        x = x*10 + ch[poz]-'0';
        poz ++;
        if ( poz >=buf )
        fread ( ch ,1,  buf-1 , stdin) , poz = 0;

    }
//printf("%d ",x);
}
void read()
{
    int x,y,cost;
 //   scanf("%d %d\n",&n,&m);
        r ( n ) ;
        r ( m ) ;
    for(int i=1; i<=m;i++)
    {
     //   scanf("%d %d %d\n",&x,&y,&cost);
        r ( x ) ;
        r ( y ) ;
        r ( cost ) ;
        c[x][y]+=cost;
        v[x].pb(y);
        v[y].pb(x);
    }
}
int bf ()
{
    int x,y;
    memset ( viz , 0 , sizeof(viz) );
    for(q.push ( 1 ) ; !q.empty () ; q.pop ()  )
    {
        x = q.front ();
        for(int i=0 ; i<v[x].size( ) ; i++)
        {
            y = v[x][i] ;
            if ( viz [ y ] != 0 || c[x][y] ==f[x][y])
            continue;
            if ( viz[y] == 0)
            {
                viz[ y ] == 1;
                t [ y ] = x;
                q.push ( y ) ;
                if ( y == n )
                {
                    while ( !q.empty() )
                    q.pop();
                    return 1;
                }
            }
        }
    }
    return 0;
}
inline int min (int x, int y ) { if (x<y) return x ; return y;}
void solve()
{
    int fmin,fmax=0;
     while ( bf () )
    {
        fmin = INF;
        for (int nod = n ; nod!=1 ; nod = t[nod])
        fmin = min ( fmin , c[ t[nod] ] [ nod ] - f[ t[nod] ] [ nod ] );
        for(int nod = n ; nod!=1 ; nod = t[nod])
        {
            f[ nod ] [ t[nod]  ]-=fmin;
            f [ t[nod] ] [ nod ]+=fmin;
        }
        fmax+=fmin;
    }
    printf( "%d ",fmax ) ;
}
int main()
{
    freopen("maxflow.in","r",stdin);
    freopen("maxflow.out","w",stdout);
    fread ( ch ,1,  buf-1 , stdin) , poz = 0;
    read();
    solve();
    return 0;
}