Cod sursa(job #616320)

Utilizator BitOneSAlexandru BitOne Data 12 octombrie 2011 11:51:11
Problema Flux maxim de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.24 kb
#include <queue>
#include <vector>
#include <fstream>
#include <cstdlib>
#define N_MAX 361
#define bufferMax 8192
#define oo 536870912
#define pr pair< int, int >

using namespace std;
int bufferIndex=-1;
char buffer[bufferMax];
int N, source, sink;
bool was[N_MAX];
int d[N_MAX], f[N_MAX];
int C[N_MAX][N_MAX], F[N_MAX][N_MAX];
vector< pr > G[N_MAX];
vector< pr >::const_iterator it, iend;
class cmp
{
	public : bool operator() (const int& x, const int& y) const { return d[x] > d[y]; }
};
priority_queue< int, vector<int>, cmp > pQ;
inline int _min( int x, int y ) { return x <= y ? x : y; }
bool findPath()
{
	int x, i;
	for( i=1; i <= N; ++i )
		d[i]=oo, f[i]=-1;
	f[source]=-2;
	d[source]=0;
	for( pQ.push(source); !pQ.empty(); )
	{
		x=pQ.top(); pQ.pop();
		was[x]=false;
		for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
		{
			if( source == it->first )
				continue;
			if( d[it->first] > d[x]+it->second && C[x][it->first] > F[x][it->first] )
			{
				f[it->first]=x;
				d[it->first]=d[x]+it->second;
				if( false == was[it->first] )
				{
					pQ.push( it->first );
					was[it->first]=true;
				}				
			}
		}
	}
	return -1 != f[sink];
}
void Read( istream& in, int& x )
{
	if( -1 == bufferIndex )
	{
		bufferIndex=0;
		in.read( buffer, bufferMax ); 
	}
	int sgn=1;
	while( buffer[bufferIndex] < '0' || buffer[bufferIndex] > '9' )
	{
		if( '-' == buffer[bufferIndex] )
			sgn*=-1;
		if( ++bufferIndex == bufferMax )
		{
			bufferIndex=0;
			in.read( buffer, bufferMax ); 
		}
	} 
	for( x=0; buffer[bufferIndex] >= '0' && buffer[bufferIndex] <= '9'; )
	{
		x=x*10+buffer[bufferIndex]-'0';
		if( ++bufferIndex == bufferMax )
		{
			bufferIndex=0;
			in.read( buffer, bufferMax ); 
		}
	}
	x*=sgn;
}
int main( void )
{
	long long s;
	int M, x, y, c, dis;
	ifstream in( "fmcm.in" );
	for(Read(in, N), Read( in, M), Read(in,source), Read(in, sink); M; --M )
	{
		Read(in, x); Read(in, y); Read(in, c); Read(in, dis);
		G[x].push_back( pr( y, dis ) );
		G[y].push_back( pr( x, -dis ) );
		C[x][y]=c;
	}
	for( s=0; findPath(); s+=1LL*c*d[sink] )
	{
		c=oo;
		for( x=sink; -2 != f[x]; x=f[x] )
			c=_min( c, C[f[x]][x]-F[f[x]][x] );
		for( x=sink; -2 != f[x]; x=f[x] )
			F[f[x]][x]+=c, F[x][f[x]]-=c;		
	}
	ofstream out( "fmcm.out" );
	out<<s<<'\n';
	return EXIT_SUCCESS;
}