Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 10005;
const ll INF = 1e18;
struct Edge {
  int from, to;
  ll dist;
  Edge(int u, int v, ll d) : from(u), to(v), dist(d){};
};
struct HeapNode {
  ll d;
  int u;
  bool operator<(const HeapNode &rhs) const { return d > rhs.d; }
};
struct Dijk {
  int n, m, M;
  vector<Edge> edges;
  vector<int> G[maxn];
  bool done[maxn];
  ll d[maxn];
  int p[maxn];
  void init() {
    for (int i = 1; i <= maxn; i++)
      G[i].clear();
    edges.clear();
  }
  void AddEdge(int from, int to, int dist) {
    edges.push_back(Edge(from, to, dist));
    M = edges.size();
    G[from].push_back(M - 1);
  }
  void dijkstra(int s) {
    priority_queue<HeapNode> Q;
    for (int i = 1; i <= n; i++)
      d[i] = INF;
    d[s] = 0;
    memset(done, 0, sizeof done);
    Q.push((HeapNode){0, s});
    while (!Q.empty()) {
      HeapNode x = Q.top();
      Q.pop();
      int u = x.u;
      if (done[u])
        continue;
      done[u] = true;
      for (int i = 0; i < (int)G[u].size(); i++) {
        Edge &e = edges[G[u][i]];
        if (d[e.to] > d[u] + e.dist) {
          d[e.to] = d[u] + e.dist;
          p[e.to] = G[u][i];
          Q.push((HeapNode){d[e.to], e.to});
        }
      }
    }
  }
  void Domain() {
    int s, u, v, q, k;
    ll dis;
    scanf("%d", &n);
    init();
    for (int i = 1; i <= n - 1; i++) {
      scanf("%d%d%lld", &u, &v, &dis);
      AddEdge(u, v, dis);
      AddEdge(v, u, dis);
    }
    scanf("%d%d", &q, &k);
    dijkstra(k);
    for (int i = 1; i <= q; i++) {
      scanf("%d%d", &u, &v);
      printf("%lld\n", d[u] + d[v]);
    }
  }
};
int main() {
  Dijk a;
  a.Domain();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100005;
const ll INF = 1e18;
struct Edge {
  int from, to;
  ll dist;
  Edge(int u, int v, ll d) : from(u), to(v), dist(d){};
};
struct HeapNode {
  ll d;
  int u;
  bool operator<(const HeapNode &rhs) const { return d > rhs.d; }
};
struct Dijk {
  int n, m, M;
  vector<Edge> edges;
  vector<int> G[maxn];
  bool done[maxn];
  ll d[maxn];
  int p[maxn];
  void init() {
    for (int i = 1; i <= maxn; i++)
      G[i].clear();
    edges.clear();
  }
  void AddEdge(int from, int to, int dist) {
    edges.push_back(Edge(from, to, dist));
    M = edges.size();
    G[from].push_back(M - 1);
  }
  void dijkstra(int s) {
    priority_queue<HeapNode> Q;
    for (int i = 1; i <= n; i++)
      d[i] = INF;
    d[s] = 0;
    memset(done, 0, sizeof done);
    Q.push((HeapNode){0, s});
    while (!Q.empty()) {
      HeapNode x = Q.top();
      Q.pop();
      int u = x.u;
      if (done[u])
        continue;
      done[u] = true;
      for (int i = 0; i < (int)G[u].size(); i++) {
        Edge &e = edges[G[u][i]];
        if (d[e.to] > d[u] + e.dist) {
          d[e.to] = d[u] + e.dist;
          p[e.to] = G[u][i];
          Q.push((HeapNode){d[e.to], e.to});
        }
      }
    }
  }
  void Domain() {
    int s, u, v, q, k;
    ll dis;
    scanf("%d", &n);
    init();
    for (int i = 1; i <= n - 1; i++) {
      scanf("%d%d%lld", &u, &v, &dis);
      AddEdge(u, v, dis);
      AddEdge(v, u, dis);
    }
    scanf("%d%d", &q, &k);
    dijkstra(k);
    for (int i = 1; i <= q; i++) {
      scanf("%d%d", &u, &v);
      printf("%lld\n", d[u] + d[v]);
    }
  }
};
int main() {
  Dijk a;
  a.Domain();
  return 0;
}