Cod sursa(job #2033426)

Utilizator OldpugAlex Ionescu Oldpug Data 6 octombrie 2017 19:46:43
Problema Sate Scor 85
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <fstream>
#include <vector>
#include <queue>
 
int main()
{
  std::ifstream in("sate.in");
 
  int n, m, x, y;
  in >> n >> m >> x >> y;
 
  auto graph = new std::vector<std::pair<int, int>>[n + 1];
  for (auto i = 0; i < m; ++i)
  {
    int i1, i2, dst;
    in >> i1 >> i2 >> dst;
 
    graph[i1].push_back({ i2, dst });
    graph[i2].push_back({ i1, dst });
  }
 
  auto dst = new int[n + 1];
  auto seen = new bool[n + 1];
  std::queue<int> que;
  
  que.push(x);
  while (!que.empty())
  {
    auto crr = que.front();
    que.pop();
 
    for (auto temp : graph[crr])
      if (!seen[temp.first])
      {
        seen[temp.first] = true;
        que.push(temp.first);
        dst[temp.first] = crr < temp.first ? dst[crr] + temp.second : dst[crr] - temp.second;
      }
  }
 
  std::ofstream("sate.out") << dst[y];
}