Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int(i) = (a); (i) < (int)(b); (i)++)
#define RREP(i, a, b) for (int(i) = (a); (i) >= (int)(b); (i)--)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;

template <typename T> class Edge {
public:
  int to;
  T cost;

  Edge() {}
  Edge(int t, T c) {
    this->to = t;
    this->cost = c;
  }
  bool operator<(Edge e) { return this->cost < e.cost; }
  bool operator>(Edge e) { return this->cost > e.cost; }
};

template <typename T> class Graph_list {
public:
  vector<vector<Edge<T>>> list;

  Graph_list(int n);
  void add(int f, int t, T c);
  void add(int f, int t);
  int size();
  vector<Edge<T>> operator[](int i) { return list[i]; }
};

template <typename T> Graph_list<T>::Graph_list(int n) {
  list = vector<vector<Edge<T>>>(n + 1, vector<Edge<T>>());
}

template <typename T> void Graph_list<T>::add(int f, int t, T c) {
  list[f].push_back(Edge<T>(t, c));
}
template <typename T> void Graph_list<T>::add(int f, int t) {
  list[f].push_back(Edge<T>(t, 1));
}
template <typename T> int Graph_list<T>::size() { return (int)list.size() - 1; }

template <typename T> vector<T> bfs_graph(Graph_list<T> &g, int s) {
  const auto INF = numeric_limits<T>::max();
  vector<T> dist(g.size(), INF);

  queue<T> que;
  que.push(s);
  dist[s] = 0;
  while (que.size()) {
    int p = que.front();
    que.pop();
    for (auto e : g[p]) {
      if (dist[e.to] == INF) {
        dist[e.to] = dist[p] + e.cost;
        que.push(e.to);
      }
    }
  }
  return dist;
}

int main() {
  int n;
  cin >> n;
  Graph_list<ll> G(n);
  REP(i, 0, n - 1) {
    int a, b, c;
    cin >> a >> b >> c;
    G.add(a, b, c);
    G.add(b, a, c);
  }

  int q, k;
  cin >> q >> k;
  vector<ll> d = bfs_graph<ll>(G, k);
  REP(i, 0, q) {
    int x, y;
    cin >> x >> y;
    cout << d[x] + d[y] << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int(i) = (a); (i) < (int)(b); (i)++)
#define RREP(i, a, b) for (int(i) = (a); (i) >= (int)(b); (i)--)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;

template <typename T> class Edge {
public:
  int to;
  T cost;

  Edge() {}
  Edge(int t, T c) {
    this->to = t;
    this->cost = c;
  }
  bool operator<(Edge e) { return this->cost < e.cost; }
  bool operator>(Edge e) { return this->cost > e.cost; }
};

template <typename T> class Graph_list {
public:
  vector<vector<Edge<T>>> list;

  Graph_list(int n);
  void add(int f, int t, T c);
  void add(int f, int t);
  int size();
  vector<Edge<T>> operator[](int i) { return list[i]; }
};

template <typename T> Graph_list<T>::Graph_list(int n) {
  list = vector<vector<Edge<T>>>(n + 1, vector<Edge<T>>());
}

template <typename T> void Graph_list<T>::add(int f, int t, T c) {
  list[f].push_back(Edge<T>(t, c));
}
template <typename T> void Graph_list<T>::add(int f, int t) {
  list[f].push_back(Edge<T>(t, 1));
}
template <typename T> int Graph_list<T>::size() { return (int)list.size(); }

template <typename T> vector<T> bfs_graph(Graph_list<T> &g, int s) {
  const auto INF = numeric_limits<T>::max();
  vector<T> dist(g.size(), INF);

  queue<T> que;
  que.push(s);
  dist[s] = 0;
  while (que.size()) {
    int p = que.front();
    que.pop();
    for (auto e : g[p]) {
      if (dist[e.to] == INF) {
        dist[e.to] = dist[p] + e.cost;
        que.push(e.to);
      }
    }
  }
  return dist;
}

int main() {
  int n;
  cin >> n;
  Graph_list<ll> G(n);
  REP(i, 0, n - 1) {
    int a, b, c;
    cin >> a >> b >> c;
    G.add(a, b, c);
    G.add(b, a, c);
  }

  int q, k;
  cin >> q >> k;
  vector<ll> d = bfs_graph<ll>(G, k);
  REP(i, 0, q) {
    int x, y;
    cin >> x >> y;
    cout << d[x] + d[y] << endl;
  }

  return 0;
}