Pagini recente » Cod sursa (job #3163197) | Cod sursa (job #147973) | Cod sursa (job #1499348) | Cod sursa (job #2250207) | Cod sursa (job #2798390)
#include <stdio.h>
#include <vector>
#include <queue>
#define INF 1000000000
#define MAX 30001
struct Andrei {
int val, cost;
};
std::vector< Andrei > noduri[ MAX + 1 ];
int n, m, X, Y;
bool ok[ MAX + 1 ];
int dp[ MAX + 1 ];
std::queue<int> q;
void Lee() {
for( int i = 0; i <= n; i++ )
dp[ i ] = INF;
dp[ X ] = 0;
ok[ X ] = 1;
q.push( X );
while( !q.empty() ) {
int nod = q.front();
q.pop();
int Cost;
ok[ nod ] = 1;
int right = noduri[ nod ].size();
for( int i = 0; i < right; i++ ) {
if( nod < noduri[ nod ][ i ].val )
Cost = noduri[ nod ][ i ].cost;
else Cost = -noduri[ nod ][ i ].cost;
if( !ok[ noduri[ nod ][ i ].val ] && dp[ noduri[ nod ][ i ].val ] > dp[ nod ] + Cost ) {
dp[ noduri[ nod ][ i ].val ] = dp[ nod ] + Cost;
ok[ noduri[ nod ][ i ].val ] = 1;
q.push( noduri[ nod ][ i ].val );
// printf( "cost = %d pt noul %d de la nodul %d\n", dp[ noduri[ nod ][ i ].val ], noduri[ nod ][ i ].val, nod );
}
}
}
}
int main()
{
FILE *fin = fopen( "sate.in", "r" );
fscanf( fin, "%d %d %d %d", &n, &m, &X, &Y );
int x, y, cost;
for( int i = 0; i < m; i++ ) {
fscanf( fin, "%d %d %d", &x, &y, &cost );
noduri[ x ].push_back( { y, cost } );
noduri[ y ].push_back( { x, cost } );
}
fclose( fin );
Lee();
FILE *fout = fopen( "sate.out", "w" );
fprintf( fout, "%d\n", dp[ Y ] );
fclose( fout );
return 0;
}