Cod sursa(job #572291)

Utilizator raica_cristiraica dumitru cristian raica_cristi Data 5 aprilie 2011 10:29:59
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.11 kb
#include<stdio.h>
#include<vector>
#include<queue>
#include<bitset>
#include<string.h>

#define dim 1010
#define pb push_back
#define buf 150
#define INF 0x3f3f3f3f

using namespace std;

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

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

        while ( isdigit ( ch[poz]) )
        {
            x = x*10 + ch[poz] - '0';
            if ( ++poz == buf )
            fread (ch , 1 , buf , stdin) , poz = 0;

        }

//printf("%d ",x);
}
void read()
{int x,y,cost;
       r ( n ) ;
       r ( m ) ;
       //  scanf("%d %d\n",&n,&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(int i=1; i<=n;i++)
    viz[i] = 0;
    viz[1]=1;
    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] )
        {
            viz[ y ] = 1;
            t [ y ] = x ;
            if ( y!=n )
            q.push(y);
        }
        }
    }
    return viz[n];
}
int min ( int x, int y )
{
    if ( x<y)
    return x;
    return y;
}
void solve()
{
    int flux = 0,fmin;
    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[t[nod]][nod]+=fmin;
            f[nod][t[nod]]-=fmin;
        }
        flux+=fmin;
}
printf("%d\n",flux);
}

int main()
{
    freopen("maxflow.in","r",stdin);
    freopen("maxflow.out","w",stdout);
    read();
    solve();
    return 0;
}