Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, s, n) for (int i = s; i < n; i++)
#define repe(i, s, n) for (int i = s; i <= n; i++)
typedef long long ll;
static const ll MOD = 1e9 + 7;
static const ll INF = (ll)1 << 62;

// input sample
// 5
// 0 3 2 3 3 1 1 2
// 1 2 0 2 3 4
// 2 3 0 3 3 1 4 1
// 3 4 2 1 0 1 1 4 4 3
// 4 2 2 1 3 3

// output sample
// shortest cost of each node
// 0 0
// 1 2
// 2 2
// 3 1
// 4 3
// parent id of each node
// 0 - 1
// 1 0
// 2 3
// 3 0
// 4 2
static const int MAX = 100000;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
vector<pair<int, int>> adj[MAX];

ll pre[MAX];
ll p[MAX];
ll d[MAX];
ll color[MAX];
priority_queue<pair<int, int>> PQ;
ll n;
void dijkstra(int s) {
  repe(i, 1, n) {
    d[i] = INF;
    pre[i] = INF;
    color[i] = WHITE;
  }

  d[s] = 0;
  p[s] = -1;
  PQ.push(make_pair(s, s)); // ?reverse?
  color[s] = GRAY;

  while (!PQ.empty()) {
    pair<ll, ll> f = PQ.top();
    PQ.pop();
    ll u = f.second;

    color[u] = BLACK;

    if (d[u] < f.first * (-1))
      continue;

    rep(j, 0, adj[u].size()) {
      ll v = adj[u][j].first;
      if (color[v] == BLACK)
        continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        PQ.push(make_pair(d[v] * (-1), v));
        p[v] = u;
        color[v] = GRAY;
      }
    }
  }
  // rep(i, 0, n) cout << i << " " << (d[i] == INF ? -1 : d[i]) << endl;
  // rep(i, 0, n) cout << i << " " << (p[i] == INF ? -1 : p[i]) << endl;
}

vector<int> get_path(int t) {
  vector<int> path;
  for (; t != -1; t = p[t])
    path.push_back(t);
  reverse(path.begin(), path.end());
  return path;
}

int main() {
  cin >> n;
  rep(i, 0, n - 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    adj[a].push_back(make_pair(b, c));
    adj[b].push_back(make_pair(a, c));
  }
  ll q, k;
  cin >> q >> k;

  dijkstra(k);

  rep(i, 0, q) {
    ll x, y;
    cin >> x >> y;
    cout << d[x] + d[y] << endl;
  }

  // int k, u, v, c;
  // cin >> n;
  // rep(i, 0, n) {
  //	cin >> u >> k;
  //	rep(j, 0, k) {
  //		cin >> v >> c;
  //		adj[u].push_back(make_pair(v, c));
  //	}
  // }
  // dijkstra(0);
  // pass start index;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, s, n) for (int i = s; i < n; i++)
#define repe(i, s, n) for (int i = s; i <= n; i++)
typedef long long ll;
static const ll MOD = 1e9 + 7;
static const ll INF = (ll)1 << 62;

// input sample
// 5
// 0 3 2 3 3 1 1 2
// 1 2 0 2 3 4
// 2 3 0 3 3 1 4 1
// 3 4 2 1 0 1 1 4 4 3
// 4 2 2 1 3 3

// output sample
// shortest cost of each node
// 0 0
// 1 2
// 2 2
// 3 1
// 4 3
// parent id of each node
// 0 - 1
// 1 0
// 2 3
// 3 0
// 4 2
static const int MAX = 100001;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
vector<pair<int, int>> adj[MAX];

ll pre[MAX];
ll p[MAX];
ll d[MAX];
ll color[MAX];
priority_queue<pair<int, int>> PQ;
ll n;
void dijkstra(int s) {
  repe(i, 1, n) {
    d[i] = INF;
    pre[i] = INF;
    color[i] = WHITE;
  }

  d[s] = 0;
  p[s] = -1;
  PQ.push(make_pair(s, s)); // ?reverse?
  color[s] = GRAY;

  while (!PQ.empty()) {
    pair<ll, ll> f = PQ.top();
    PQ.pop();
    ll u = f.second;

    color[u] = BLACK;

    if (d[u] < f.first * (-1))
      continue;

    rep(j, 0, adj[u].size()) {
      ll v = adj[u][j].first;
      if (color[v] == BLACK)
        continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        PQ.push(make_pair(d[v] * (-1), v));
        p[v] = u;
        color[v] = GRAY;
      }
    }
  }
  // rep(i, 0, n) cout << i << " " << (d[i] == INF ? -1 : d[i]) << endl;
  // rep(i, 0, n) cout << i << " " << (p[i] == INF ? -1 : p[i]) << endl;
}

vector<int> get_path(int t) {
  vector<int> path;
  for (; t != -1; t = p[t])
    path.push_back(t);
  reverse(path.begin(), path.end());
  return path;
}

int main() {
  cin >> n;
  rep(i, 0, n - 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    adj[a].push_back(make_pair(b, c));
    adj[b].push_back(make_pair(a, c));
  }
  ll q, k;
  cin >> q >> k;

  dijkstra(k);

  rep(i, 0, q) {
    ll x, y;
    cin >> x >> y;
    cout << d[x] + d[y] << endl;
  }

  // int k, u, v, c;
  // cin >> n;
  // rep(i, 0, n) {
  //	cin >> u >> k;
  //	rep(j, 0, k) {
  //		cin >> v >> c;
  //		adj[u].push_back(make_pair(v, c));
  //	}
  // }
  // dijkstra(0);
  // pass start index;

  return 0;
}