Cod sursa(job #810845)

Utilizator MtkMarianHagrSnaf MtkMarian Data 11 noiembrie 2012 08:22:24
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;

#define NMAX 1005
#define INF 2e7;

int n , m , x ,y , z ,flx ,fluxmin;
vector< int > g[NMAX] ;
queue < int  > que ;

int c[NMAX][NMAX]={0} , viz[NMAX] , t[NMAX] , flux[NMAX][NMAX] = { 0} ;

int bfs()
{
	for(int i=1 ;i<=n; ++i ) viz[i]=0 ;
	
	while(que.size())
		que.pop();

	que.push( 1 );
	
	while( que.size() ) 
	{

		int x = que.front();		
	    que.pop();

		viz[ x ] = 1 ;
			
		for(int j = 0 ; j < g [ x ].size() ; ++j )
		{
			int y = g [ x ][ j ] ; 
				
			if( !viz[ y ] && ( flux[ x ][ y ] < c[ x ][ y ] ) )
			{
				que.push(y);
				t[ y ] = x ;				
				if( y == n ) return 1 ;							
			}
		}
		
	}
	return 0;

}

void citeste()
{
	scanf("%d %d",&n,&m);
	for(int i = 1 ; i <= m ;++i)
	{
		scanf("%d %d %d",&x,&y,&z);
		c[x][y] +=z;
		g[x].push_back ( y );
		g[y].push_back( x );
	}

}

int main()
{
	freopen("maxflow.in", "r",stdin);
	freopen("maxflow.out","w",stdout);
	citeste();
	flx=0;
	while( bfs() )
	{
			fluxmin=INF;

			for(int nod = n ; nod != 1 ; nod = t[nod])
				fluxmin =  min ( fluxmin , c[ t [ nod ] ] [ nod ] - flux [ t[nod] ] [ nod ] ) ;

			for(int nod = n ; nod != 1 ; nod = t [ nod ] )
			{
				flux [t[nod]] [nod] += fluxmin;
				flux [nod] [ t[nod] ] -= fluxmin;
			}

			flx += fluxmin ;
	}

	printf("%d",flx);
	
	return 0;
}