Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define Rep(b, e, i) for (int i = b; i <= e; i++)
#define Repr(e, b, i) for (int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n - 1, i)
#define repr(n, i) Repr(n - 1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(v) push_back(v)
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define bitcnt(x) __builtin_popcount(x)
#define fst first
#define snd second

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> intP;
typedef std::priority_queue<int> IntPrioQueue; // Z->A
typedef std::priority_queue<int, std::vector<int>, std::greater<int>>
    IntReversePrioQueue; // A->Z

// vector の中身を出力
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}

const ll INF = 1e18;

typedef pair<ll, int> P;

const int MAX_V = 200;

struct edge {
  int to;
  ll cost;
};

// V := number of nodes
// G := adj
int V;
vector<edge> G[MAX_V];
ll d[MAX_V];
void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    ll v = p.second;
    if (d[v] < p.first)
      continue;
    rep((int)G[v].size(), i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

void solve(void) {
  int N;
  cin >> N;
  V = N;
  rep(N - 1, i) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    G[a].push_back(edge{b, c});
    G[b].push_back(edge{a, c});
  }
  int Q, K;
  cin >> Q >> K;
  K--;
  dijkstra(K);
  rep(Q, i) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    cout << d[x] + d[y] << endl;
  }
}

int main(void) {
  solve();
  // cout << "yui(*-v・)yui" << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define Rep(b, e, i) for (int i = b; i <= e; i++)
#define Repr(e, b, i) for (int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n - 1, i)
#define repr(n, i) Repr(n - 1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(v) push_back(v)
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define bitcnt(x) __builtin_popcount(x)
#define fst first
#define snd second

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> intP;
typedef std::priority_queue<int> IntPrioQueue; // Z->A
typedef std::priority_queue<int, std::vector<int>, std::greater<int>>
    IntReversePrioQueue; // A->Z

// vector の中身を出力
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}

const ll INF = 1e18;

typedef pair<ll, int> P;

const int MAX_V = 100010;

struct edge {
  int to;
  ll cost;
};

// V := number of nodes
// G := adj
int V;
vector<edge> G[MAX_V];
ll d[MAX_V];
void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    ll v = p.second;
    if (d[v] < p.first)
      continue;
    rep((int)G[v].size(), i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

void solve(void) {
  int N;
  cin >> N;
  V = N;
  rep(N - 1, i) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    G[a].push_back(edge{b, c});
    G[b].push_back(edge{a, c});
  }
  int Q, K;
  cin >> Q >> K;
  K--;
  dijkstra(K);
  rep(Q, i) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    cout << d[x] + d[y] << endl;
  }
}

int main(void) {
  solve();
  // cout << "yui(*-v・)yui" << endl;
  return 0;
}