Pagini recente » Cod sursa (job #1503756) | Monitorul de evaluare | Diferente pentru tabele-hash-prezentare-detaliata intre reviziile 21 si 22 | Istoria paginii utilizator/deejayfreeak | Cod sursa (job #456543)
Cod sursa(job #456543)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on May 15, 2010, 7:54 PM
*/
#include <queue>
#include <cstdlib>
#include <fstream>
#include <algorithm>
#define Nmax 400
#define oo 0x3f3f3f3f
/*
*
*/
using namespace std;
typedef pair< int, int > pr;
int N, source, sink;
int d[Nmax], f[Nmax];
int C[Nmax][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 bool find_path( void )
{
int x;
bool inH[ Nmax ]={0};
fill( d+1, d+N+1, oo );
d[source]=0;
for( pQ.push(source); !pQ.empty(); )
{
x=pQ.top(); pQ.pop();
inH[x]=false;
for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
{
if( source == it->first )
continue;
if( C[x][it->first] > 0 && d[x]+it->second < d[it->first] )
{
f[it->first]=x;
d[it->first]=d[x]+it->second;
if( false == inH[it->first] )
{
pQ.push(it->first);
inH[it->first]=true;
}
}
}
}
return oo != d[sink];
}
int main( void )
{
int M, x, y, c;
ifstream in( "fmcm.in" );
for( in>>N>>M>>source>>sink; M; --M )
{
in>>x>>y;
in>>C[x][y]>>c;
G[x].push_back( pr( y, c ) );
G[y].push_back( pr( x, -c ) );
}
for( x=0; find_path(); )
{
c=oo;
for( y=sink; f[y]; y=f[y] )
if( c > C[f[y]][y] )
c=C[f[y]][y];
for( y=sink; f[y]; y=f[y] )
{
C[f[y]][y]-=c;
C[y][f[y]]+=c;
}
x+=c*d[sink];
}
ofstream out( "fmcm.out" );
out<<x<<'\n';
return EXIT_SUCCESS;
}