Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define lowbit(x) ((x) & (-x))
#define INF 0x3f3f3f3f
#define mem(a, b) memset(a, b, sizeof(a))
#define GE() printf(">----------\n")
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define MP pair<int, int>
typedef long long LL;
const int mod = 1000000007;
const LL inf = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10005;

int n;
LL dis[maxn];
int inq[maxn];
vector<pair<int, int>> E[maxn];

struct Spfa {

  void init() {
    for (int i = 0; i < maxn; i++) {
      dis[i] = inf;
      E[i].clear();
      inq[i] = 0;
    }
  }

  int spfa(int s) {
    queue<int> Q;
    dis[s] = 0;
    inq[s] = 1;
    Q.push(s);
    while (!Q.empty()) {
      int u = Q.front();
      Q.pop();
      inq[u] = 0;
      for (int i = 0; i < E[u].size(); i++) {
        int v = E[u][i].first;
        if (dis[v] > dis[u] + E[u][i].second) {
          dis[v] = dis[u] + E[u][i].second;
          if (inq[v])
            continue;
          Q.push(v);
          inq[v] = 1;
        }
      }
    }
    return dis[n];
  }
};

int main() {
  while (~scanf("%d", &n)) {
    Spfa spfa;
    spfa.init();
    int u, v, w;
    for (int i = 0; i < n - 1; i++) {
      scanf("%d%d%d", &u, &v, &w);
      E[u].push_back(make_pair(v, w));
      E[v].push_back(make_pair(u, w));
    }
    int q, k;
    scanf("%d%d", &q, &k);
    spfa.spfa(k);
    for (int i = 0; i < q; i++) {
      scanf("%d%d", &u, &v);
      printf("%lld\n", dis[u] + dis[v]);
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define lowbit(x) ((x) & (-x))
#define INF 0x3f3f3f3f
#define mem(a, b) memset(a, b, sizeof(a))
#define GE() printf(">----------\n")
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define MP pair<int, int>
typedef long long LL;
const int mod = 1000000007;
const LL inf = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100005;

int n;
LL dis[maxn];
int inq[maxn];
vector<pair<int, int>> E[maxn];

struct Spfa {

  void init() {
    for (int i = 0; i < maxn; i++) {
      dis[i] = inf;
      E[i].clear();
      inq[i] = 0;
    }
  }

  int spfa(int s) {
    queue<int> Q;
    dis[s] = 0;
    inq[s] = 1;
    Q.push(s);
    while (!Q.empty()) {
      int u = Q.front();
      Q.pop();
      inq[u] = 0;
      for (int i = 0; i < E[u].size(); i++) {
        int v = E[u][i].first;
        if (dis[v] > dis[u] + E[u][i].second) {
          dis[v] = dis[u] + E[u][i].second;
          if (inq[v])
            continue;
          Q.push(v);
          inq[v] = 1;
        }
      }
    }
    return dis[n];
  }
};

int main() {
  while (~scanf("%d", &n)) {
    Spfa spfa;
    spfa.init();
    int u, v, w;
    for (int i = 0; i < n - 1; i++) {
      scanf("%d%d%d", &u, &v, &w);
      E[u].push_back(make_pair(v, w));
      E[v].push_back(make_pair(u, w));
    }
    int q, k;
    scanf("%d%d", &q, &k);
    spfa.spfa(k);
    for (int i = 0; i < q; i++) {
      scanf("%d%d", &u, &v);
      printf("%lld\n", dis[u] + dis[v]);
    }
  }
  return 0;
}