Cod sursa(job #410225)

Utilizator alexandru92alexandru alexandru92 Data 4 martie 2010 10:39:28
Problema Flux maxim de cost minim Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 3.06 kb
#include <queue>
#include <vector>
#include <fstream>
#define Nmax 1000
#define INF 0x3f3f3f3f

/*
 *
 */
using namespace std;
int N, NH, S, source, sink;
int H[Nmax], Position[Nmax];
int C[Nmax][Nmax], Cost[Nmax][Nmax];
vector< int > d;
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;
	for( ; ; )
	{
		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[k]], Position[H[f]] );
		swap( H[k], H[f] );
		k=f;
		f/=2;
	}
}
inline int pop( void )
{
	int r=H[1];
	Position[H[1]]=Position[H[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 )
{
	int i;
	for( 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 );
	d.assign( 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] )
				{
					inQ[*it]=true;
					Q.push( *it );
				}
			}
	}
	S=d[sink];
}
int find_path( void )
{
	doo();
	int x;
	/*Dijkstra+Heap*/
	d.assign( N, INF );
	vector< bool > inHeap( N );
	vector< int > father( N, -1 ), c( N );
	d[source]=0;
	c[source]=INF;
	push( source );
	inHeap[source]=true;
	while( NH )
	{
		x=pop();
		inHeap[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] )
			{
				father[*it]=x;
				d[*it]=d[x]+Cost[x][*it];
				c[*it]=min( c[x], C[x][*it] );
				push( *it );
			}
	}
	if( -1 == father[sink] )
		return INF;
	for( x=sink; -1 != father[x]; x=father[x] )
	{
		C[father[x]][x]-=c[sink];
		if( 0 == C[x][father[x]] )
		{
			G[x].push_back( father[x] );
			Cost[x][father[x]]=-Cost[father[x]][x];
		}
		C[x][father[x]]+=c[sink];
	}
	S+=d[sink];
	return c[sink]*S;
}
int MaxFlowMinCut( void )
{
	BellmanFord();
	int path_c, s=0;
	for( path_c=find_path(); INF != 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);
	d.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;
}