Cod sursa(job #410412)

Utilizator alexandru92alexandru alexandru92 Data 4 martie 2010 13:01:15
Problema Flux maxim de cost minim Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 3.14 kb
#include <queue>
#include <vector>
#include <fstream>
#define Nmax 351
#define INF 0x3f3f3f3f

/*
 *
 */
using namespace std;
int N, NH, S, source, sink;
int C[Nmax][Nmax], Cost[Nmax][Nmax];
int H[Nmax], d[Nmax], father[Nmax], Position[Nmax];
vector< vector< int > > G;
vector< int >::const_iterator it, iend;
inline void swap( int& x, int& y )
{
	int aux=x;
	x=y;
	y=aux;
}
inline const int& min( const int& x, const int& y )
{
	if( x < y )
		return x;
	return y;
}
void DownHeap( int k )
{
	int son;
	while( 1 )
	{
		son=2*k;
		if( son > NH )
			return;
		if( son < NH && d[H[son]] > d[H[son+1]] )
			++son;
		if( d[H[k]] <= d[H[son]] )
			return;
		swap( Position[H[k]], Position[H[son]] );
		swap( H[k], H[son] );
		k=son;
	}
}
void UpHeap( int k )
{
	int key=d[H[k]], f=k/2;
	while( k > 1 && key < d[H[f]] )
	{
		swap( Position[H[f]], Position[H[f]] );
		swap( H[f], H[k] );
		k=f;
		f/=2;
	}
}
inline int pop( void )
{
	int r=H[1];
	Position[1]=Position[NH];
	H[1]=H[NH];
	--NH;
	DownHeap( 1 );
	return r;
}
inline void push( int k )
{
	H[++NH]=k;
	Position[k]=NH;
	UpHeap( NH );
}
void doo( void )
{
	for( int i=0; i < N; ++i )
		if( INF != d[i] )
			for( it=G[i].begin(), iend=G[i].end(); it < iend; ++it )
				if( INF != d[*it] )
					Cost[i][*it]+=d[i]-d[*it];
}
void BellmanFord( void )
{
	int x;
	queue< int > Q;
	vector< bool > inQ( N );
	fill( d, d+N, INF );
	d[source]=0;
	Q.push( source );
	inQ[source]=true;
	while( !Q.empty() )
	{
		x=Q.front(); Q.pop();
		inQ[x]=false;
		for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
			if( C[x][*it] > 0 && d[x]+Cost[x][*it] < d[*it] )
			{
				d[*it]=d[x]+Cost[x][*it];
				if( !inQ[*it] )
				{
					Q.push(*it);
					inQ[*it]=true;
				}
			}
	}
	S=d[sink];
}
int find_path( void )
{
	doo();
	NH=0;
	int x, min_c;
	for( x=0; x < N; ++x )
	{
		d[x]=INF;
		father[x]=-1;
		H[++NH]=x;
		Position[x]=NH;
	}
	d[source]=0;
	H[1]=source, H[source+1]=0;
	Position[source]=1, Position[0]=source+1;
	while( NH && d[H[1]] < INF )
	{
		x=pop();
		for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
			if( C[x][*it] > 0 && d[x]+Cost[x][*it] < d[*it] )
			{
				father[*it]=x;
				d[*it]=d[x]+Cost[x][*it];
				UpHeap( Position[*it] );
			}
	}
	if( -1 == father[sink] )
		return 0;
	father[source]=-1;
	min_c=C[ father[sink] ][ sink ];
	for( x=father[sink]; -1 != father[x]; x=father[x] )
		min_c=min( min_c, C[ father[x] ][ x ] );
	for( x=sink; -1 != father[x]; x=father[x] )
	{
		C[father[x]][x]-=min_c;
		if( 0 == C[x][father[x]] )
		{
			G[x].push_back( father[x] );
			Cost[x][father[x]]=-Cost[father[x]][x];
		}
		C[x][father[x]]+=min_c;
	}
	S+=d[sink];
	return min_c*S;
}
int MaxFlowMinCut( void )
{
	int path_c, s=0;
	for( path_c=find_path(); path_c; s+=path_c, path_c=find_path() );
	return s;
}
int main( void )
{
	int M, x, y;
	ifstream in( "fmcm.in" );
	in>>N>>M>>source>>sink;
	--source, --sink;
	G.resize(N);
	for( ; M; --M )
	{
		in>>x>>y;
		--x, --y;
		in>>C[x][y]>>Cost[x][y];
		G[x].push_back( y );
	}
	ofstream out( "fmcm.out" );
	out<<MaxFlowMinCut();
	return 0;
}