Pagini recente » Cod sursa (job #460036) | Cod sursa (job #1456643) | Cod sursa (job #1071890) | Cod sursa (job #484200) | Cod sursa (job #1712212)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
ifstream in("fmcm.in");
ofstream out("fmcm.out");
#define N 352
#define mult 2000000000
vector < int > l[N];
int n, m, s, d, x, y, z, w, dist[N], c[N][N], cst[N][N], dist2[N], dist3[N], t[N];
bool inq[N];
queue < int > q;
vector < pair < int, int > > heap;
int DIJK()
{
for( int i = 1; i <= n; ++ i )
dist2[i] = mult;
heap.push_back( make_pair( 0, s ) );
dist2[s] = 0;
make_heap( heap.begin(), heap.end() );
while( !heap.empty() )
{
pop_heap( heap.begin(), heap.end() );
int nod = heap.back().second;
int dis = -heap.back().first;
heap.pop_back();
if( dist2[nod] != dis)
continue;
for( vector < int > :: iterator it = l[nod].begin(); it != l[nod].end(); ++ it )
{
if( c[nod][*it] )
{
int niu = dist2[nod] + cst[nod][*it] + dist[nod] - dist[*it];
if( niu < dist2[*it] )
{
dist2[*it] = niu;
dist3[*it] = dist3[nod] + cst[nod][*it];
heap.push_back( make_pair( -niu, *it) );
push_heap( heap.begin(), heap.end() );
t[*it] = nod;
}
}
}
}
if( dist2[d] == mult )
return 0;
return 1;
}
void BELLMAN()
{
for( int i = 1; i <= n; ++ i )
dist[i] = mult;
dist[s] = 0;
q.push(s);
inq[s] = 1;
while( !q.empty() )
{
int x = q.front();
inq[x] = 0;
q.pop();
for( vector < int > :: iterator it = l[x].begin(); it != l[x].end(); ++ it )
{
if( c[x][*it] )
{
if( dist[x] + cst[x][y] < dist[*it] )
{
dist[*it] = dist[x] + cst[x][y];
if( inq[*it] == 0 )
{
inq[*it] = 1;
q.push(*it);
}
}
}
}
}
}
int main()
{
in >> n >> m >> s >> d;
int sol = 0;
int sol2 = 0;
for( int i = 0; i < m; ++ i )
{
in >> x >> y >> z >> w;
l[x].push_back(y);
l[y].push_back(x);
c[x][y] = z;
cst[x][y] = w;
cst[y][x] = -w;
}
BELLMAN();
while( DIJK() )
{
for( int i = 1; i <= n; ++ i )
dist[i] = dist3[i];
int fmin = mult;
for( int aux = d; t[aux]; aux = t[aux] )
{
if( c[t[aux]][aux]< fmin )
fmin = c[t[aux]][aux] ;
}
sol += fmin;
sol2 += fmin * dist3[d];
for( int aux = d; t[aux]; aux = t[aux] )
{
c[t[aux]][aux] -= fmin;
c[aux][t[aux]] += fmin;
}
}
out << sol2;
return 0;
}