Pagini recente » Cod sursa (job #33101) | Cod sursa (job #182995) | Rating pasc lucian (lwcypasc) | Cod sursa (job #846694) | Cod sursa (job #551215)
Cod sursa(job #551215)
#include <queue>
#include <vector>
#include <fstream>
#include <cstdlib>
#define N_MAX 361
#define oo 1<<20
#define pr pair< int, int >
#define mkpr make_pair< int, int >
using namespace std;
int N, source, sink;
int C[N_MAX][N_MAX];
vector< pr > G[N_MAX];
vector< int > d;
vector< pr >::const_iterator it, iend;
class cmp
{
public :
inline bool operator() ( const int& x, const int& y ) { return d[x] > d[y]; }
};
inline int _min( int x, int y ) { return ( x <= y ? x : y ); }
inline int findPath()
{
int x;
vector< bool > was( N+1 );
vector< int > f( N+1, -1 );
priority_queue< int, vector<int>, cmp > pQ;
f[source]=-2;
d.assign( N+1, oo );
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] > 0 )
{
f[it->first]=x;
d[it->first]=d[x]+it->second;
if( false == was[it->first] )
{
pQ.push(it->first);
was[it->first]=true;
}
}
}
}
if( -1 == f[sink] || oo == d[sink] )
return -oo;
int min=oo;
for( x=sink; -2 != f[x]; x=f[x] )
min=_min( min, C[f[x]][x] );
for( x=sink; -2 != f[x]; x=f[x] )
{
C[f[x]][x]-=min;
C[x][f[x]]+=min;
}
return d[sink]*min;
}
int MaxFlow()
{
int s, pathC;
for( s=0, pathC=findPath(); -oo != pathC; s+=pathC, pathC=findPath() );
return s;
}
int main( void )
{
int M, x, y, cost;
ifstream in( "fmcm.in" );
for( in>>N>>M>>source>>sink; M; --M )
{
in>>x>>y;
in>>C[x][y]>>cost;
G[x].push_back( mkpr( y, cost ) );
G[y].push_back( mkpr( x, -cost ) );
}
d.resize( N+1 );
ofstream out( "fmcm.out" );
out<<MaxFlow()<<'\n';
return EXIT_SUCCESS;
}