Pagini recente » Cod sursa (job #3255921) | Cod sursa (job #1275227) | Cod sursa (job #1149762) | Cod sursa (job #1184555) | Cod sursa (job #2667807)
#include <cstdio>
#include <cassert>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
priority_queue< pair< int, int>> q;
const int nMax = 30001;
int d[nMax];
vector< pair< int, int>> v[nMax];
void calcDist( int nod ) {
for ( pair< int, int > &i:v[ nod ] ) {
if ( i.first > nod )
q.push( { -i.second, i.first } );
else
q.push( { i.second, i.first } );
}
while ( !q.empty() ) {
int nodC, dist;
nodC = q.top().second;
dist = -q.top().first;
q.pop();
if ( d[ nodC ] == 0 || d[ nodC ] > dist ) {
d[ nodC ] = dist;
for ( pair< int, int > i : v[ nodC ] ) {
if ( i.first > nodC )
q.push( { -( dist + i.second ), i.first } );
else
q.push( { -( dist - i.second ), i.first } );
}
}
}
}
int main() {
int n, m, x, y;
FILE *fin, *fout;
fin = fopen( "sate.in", "r" );
assert( fscanf( fin, "%d%d%d%d", &n, &m, &x, &y ) == 4 );
for ( int i = 0; i < m; i++ ) {
int a, b, c;
assert( fscanf( fin, "%d%d%d", &a, &b, &c ) == 3 );
v[ a ].push_back( { b, c } );
v[ b ].push_back( { a, c } );
}
fclose( fin );
calcDist( x );
fout = fopen( "sate.out", "w" );
fprintf( fout, "%d\n", abs( d[ y ] ) );
fclose( fout );
return 0;
}