Pagini recente » Cod sursa (job #1620364) | Cod sursa (job #2415271) | Cod sursa (job #2668740) | Cod sursa (job #2513946) | Cod sursa (job #1107065)
#include <queue>
#include <bitset>
#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];
}
};
bitset< Nmax > inH;
priority_queue< int, vector< int >, cmp > pQ;
inline bool find_path( void )
{
int x;
fill( d+1, d+N+1, oo );
d[source]=0;
inH.flip(source);
for( pQ.push(source); !pQ.empty(); )
{
x=pQ.top(); pQ.pop();
inH.flip(x);
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.test(it->first) )
{
pQ.push(it->first);
inH.set(it->first);
}
}
}
}
inH&=0;
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;
}