Cod sursa(job #2798366)

Utilizator mihaipriboimihailucapriboi mihaipriboi Data 11 noiembrie 2021 11:12:48
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.25 kb
// 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;
}