Pagini recente » Cod sursa (job #884862) | Cod sursa (job #2814290) | Cod sursa (job #940561) | Cod sursa (job #1271904) | Cod sursa (job #442513)
Cod sursa(job #442513)
/*
* File: main.cpp
* Author: VirtualDemon
*
* Created on April 14, 2010, 3:51 PM
*/
#include <queue>
#include <cstdlib>
#include <fstream>
#define Nmax 400
#define SIZE 8219
#define oo 0x3f3f3f3f
/*
*
*/
using namespace std;
ifstream in;
typedef pair< int, int > pr;
int N, s, sk, ifile;
char file[SIZE];
int C[Nmax][Nmax];
int d[Nmax], f[Nmax];
vector< pr > G[Nmax];
vector< pr >::const_iterator it, iend;
class cmp
{
public:
inline bool operator() ( const int& x, const int& y ) const
{
return d[x] > d[y];
}
};
priority_queue< int, vector< int >, cmp > PQ;
inline void read( int& x )
{
int semn=1;
x=0;
while( file[ifile] < '0' || file[ifile] > '9' )
{
if( '-' == file[ifile] )
semn=-1;
if( ++ifile == SIZE )
{
in.read( file, SIZE );
ifile=0;
}
}
while( file[ifile] >= '0' && file[ifile] <= '9' )
{
x=x*10+file[ifile]-'0';
if( ++ifile == SIZE )
{
in.read( file, SIZE );
ifile=0;
}
}
x*=semn;
}
inline int find_path( void )
{
int x, c;
vector< bool > inH( N );
for( x=0; x < N; d[x]=oo, ++x );
d[s]=0;
f[s]=f[sk]=-1;
for( PQ.push(s); !PQ.empty(); )
{
x=PQ.top(); PQ.pop();
inH[x]=false;
for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
if( C[x][it->first] > 0 && d[it->first] > d[x]+it->second )
{
f[it->first]=x;
d[it->first]=d[x]+it->second;
if( !inH[it->first] )
{
PQ.push(it->first);
inH[it->first]=true;
}
}
}
if( -1 == f[sk] )
return 0;
for( x=sk, c=oo; -1 != f[x]; x=f[x] )
c=min( c, C[f[x]][x] );
for( x=sk; -1 != f[x]; x=f[x] )
{
C[x][f[x]]+=c;
C[f[x]][x]-=c;
}
return d[sk]*c;
}
inline int MaxFlowMinCost( void )
{
int s, p;
for( s=0, p=find_path(); p; s+=p, p=find_path() );
return s;
}
int main( void )
{
int M, x, y, c;
in.open( "fmcm.in" );
read(N); read(M); read(s); read(sk);
--s, --sk;
for( ; M; --M )
{
read(x), read(y), read( C[x-1][y-1] ), read(c);
--x, --y;
G[x].push_back( pr( y, c ) );
G[y].push_back( pr( x, -c ) );
}
ofstream out( "fmcm.out" );
out<<MaxFlowMinCost()<<'\n';
return EXIT_SUCCESS;
}