Pagini recente » Cod sursa (job #2711936) | Cod sursa (job #2099173) | Cod sursa (job #496973) | Cod sursa (job #3128633) | Cod sursa (job #403262)
Cod sursa(job #403262)
#include <algorithm>
#include <bitset>
#include <queue>
#include <vector>
using namespace std;
#define DIM 355
#define INF 0x3f3f3f3f
int N, M, S, D, XXX;
int val[ DIM ], t[ DIM ], cap[ DIM ][ DIM ], flx[ DIM ][ DIM ];
bitset <DIM> f;
vector < pair <int, int> > v[ DIM ];
struct cmp {
bool operator()( const int &a, const int &b ) {
return val[ a ] > val[ b ];
}
};
priority_queue < int, vector <int>, cmp > heap;
inline int bellman_ford() {
int nod, fmin;
vector < pair <int, int> > :: iterator it;
f.reset();
memset( val, INF, sizeof( val ) );
val[ S ] = 0;
heap.push( S );
while( !heap.empty() ) {
nod = heap.top();
heap.pop();
f[ nod ] = 0;
for( it = v[ nod ].begin(); it != v[ nod ].end(); ++it )
if( cap[ nod ][ it->first ] > flx[ nod ][ it->first ] && val[ nod ] + it->second < val[ it->first ] ) {
val[ it->first ] = val[ nod ] + it->second;
t[ it->first ] = nod;
if( !f[ it->first ] ) {
heap.push( it->first );
f[ it->first ] = 1;
}
}
}
if( val[ D ] != INF ) {
fmin = INF;
for( nod = D; nod != S; nod = t[ nod ] )
fmin = min( fmin, cap[ t[ nod ] ][ nod ] - flx[ t[ nod ] ][ nod ] );
for( nod = D; nod != S; nod = t[ nod ] ) {
flx[ t[ nod ] ][ nod ] += fmin;
flx[ nod ][ t[ nod ] ] -= fmin;
}
return fmin * val[ D ];
}
return 0;
}
int main() {
freopen( "fmcm.in", "r", stdin );
freopen( "fmcm.out", "w", stdout );
int x, y, ok, cost, capacitate;
scanf( "%d %d %d %d", &N, &M, &S, &D );
while( M-- ) {
scanf( "%d %d %d %d", &x, &y, &capacitate, &cost );
v[ x ].push_back( make_pair( y, cost ) );
v[ y ].push_back( make_pair( x, -cost ) );
cap[ x ][ y ] = capacitate;
}
do {
ok = bellman_ford();
XXX += ok;
}
while( ok );
printf( "%d", XXX );
return 0;
}