Pagini recente » Cod sursa (job #2377543) | Cod sursa (job #2445573) | Cod sursa (job #1337281) | Cod sursa (job #2651570) | Cod sursa (job #2798366)
// Mihai Priboi
#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
#define MAXN 100000
struct drum {
int next;
int dist;
};
vector<drum> muchii[MAXN + 1];
int noduri[MAXN + 1];
queue<int> myQueue;
int main() {
FILE *fin, *fout;
int n, m, i, x, y, a, b, c, point;
fin = fopen( "sate.in", "r" );
fscanf( fin, "%d%d%d%d", &n, &m, &x, &y );
for( i = 0; i < m; i++ ) {
fscanf( fin, "%d%d%d", &a, &b, &c );
muchii[a].push_back({b, c});
muchii[b].push_back({a, c});
}
fclose( fin );
for( i = 1; i <= n; i++ )
noduri[i] = -1;
if( x > y )
swap( x, y );
myQueue.push(x);
noduri[x] = 0;
while( myQueue.size() ) {
point = myQueue.front();
myQueue.pop();
for( i = 0; i < muchii[point].size(); i++ ) {
if( noduri[muchii[point][i].next] == -1 ) {
if( muchii[point][i].next > point )
noduri[muchii[point][i].next] = noduri[point] + muchii[point][i].dist;
else
noduri[muchii[point][i].next] = noduri[point] - muchii[point][i].dist;
myQueue.push( muchii[point][i].next );
}
}
}
fout = fopen( "sate.out", "w" );
fprintf( fout, "%d ", noduri[y] );
fclose( fout );
return 0;
}