Pagini recente » Cod sursa (job #79700) | Cod sursa (job #1422868) | Cod sursa (job #1919068) | Cod sursa (job #1086405) | Cod sursa (job #1159436)
#include <iostream>
#include <fstream>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
ifstream f("fmcm.in");
ofstream g("fmcm.out");
const int Nmax = 355;
const int oo = 0x3f3f3f3f;
typedef pair<int,int> node;
vector <int> G[Nmax];
int ante[Nmax], dist[Nmax], old_dist[Nmax], real_dist[Nmax],inQ[Nmax];
int C[Nmax][Nmax], Cost[Nmax][Nmax];
int N, M, S, D;
int solution;
struct cmp
{
bool operator()(const int &A,const int & B)const
{
return dist[A]>dist[B];
}
};
priority_queue < int, vector <int>, cmp > pq;
void read()
{
f >> N >> M >> S >> D;
for ( int i = 1; i <= M; ++i )
{
int a, b, c, z;
f >> a >> b >> c >> z;
G[a].push_back( b );
G[b].push_back( a );
C[a][b] = c;
Cost[a][b] = z;
Cost[b][a] = -z;
}
f.close();
}
inline bool Dijkstra()
{
memset( dist, oo, sizeof( dist ) );
//memset( inQ, 0, sizeof( inQ ) );
dist[S] = real_dist[S] = 0;
pq.push( S );
while( !pq.empty() )
{
int nod = pq.top();
int val = dist[nod];
pq.pop();
if( dist[nod] != val )
//continue;
for( unsigned i = 0; i < G[nod].size(); i++ )
{
int son = G[nod][i];
if( C[nod][son] )
{
int cost = dist[nod] + Cost[nod][son] + old_dist[nod] - old_dist[son];
if( cost < dist[son] )
{
ante[son] = nod;
dist[son] = cost;
real_dist[son] = real_dist[nod] + Cost[nod][son];
pq.push( son );
}
}
}
}
memcpy( old_dist, real_dist, sizeof( old_dist ) );
return ( dist[D] != oo );
}
void Edmonds_Karp()
{
while( Dijkstra() )
{
int fmin = oo;
for ( int nod = D; nod != S; nod = ante[nod] )
fmin = min( fmin, C[ ante[nod] ][nod] );
for ( int nod = D; nod != S; nod = ante[nod] )
{
C[ ante[nod] ][nod] -= fmin;
C[nod][ ante[nod] ] += fmin;
}
solution += fmin * real_dist[D];
}
}
void print()
{
g << solution << "\n";
g.close();
}
int main()
{
read();
Edmonds_Karp();
print();
return 0;
}