Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef pair<ll, ll> pll;

ll n = 0, m = 0, k = 0, ans = 0, sum = 0, cnt = 0, temp = 0;
string s;
bool flag;

//--------------------------------------------------------------------------

#define MAX 1000

#define INF 11111111111111111
#define NOT 0
#define VIS 1
#define COMP 2

ll stt[MAX];
ll mini[MAX];
vector<pll> edge[MAX];
priority_queue<pll, vector<pll>, greater<pll>> pq;

void dijkstra(ll start) {
  pll min;
  FOR(i, 1, n + 1) {
    stt[i] = NOT;
    mini[i] = INF;
  }
  mini[start] = 0;
  pq.push(make_pair(0, start));

  while (!pq.empty()) {
    min = pq.top();
    pq.pop();
    m = min.second;
    stt[m] = COMP;

    if (mini[m] < min.first)
      continue;

    FOR(i, 0, edge[m].size()) {
      k = edge[m][i].first;
      if (stt[k] != COMP) {
        if (edge[m][i].second + mini[m] < mini[k]) {
          mini[k] = edge[m][i].second + mini[m];
          stt[k] = VIS;
          pq.push(make_pair(mini[k], k));
        }
      }
    }
  }
}

int main(void) {
  ll a, b, c, q, x, y;
  cin >> n;

  FOR(i, 0, n - 1) {
    cin >> a >> b >> c;
    edge[a].push_back(make_pair(b, c));
    edge[b].push_back(make_pair(a, c));
  }

  cin >> q >> k;

  dijkstra(k);

  FOR(i, 0, q) {
    cin >> x >> y;
    cout << mini[x] + mini[y] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef pair<ll, ll> pll;

ll n = 0, m = 0, k = 0, ans = 0, sum = 0, cnt = 0, temp = 0;
string s;
bool flag;

//--------------------------------------------------------------------------

#define MAX 110000

#define INF 11111111111111111
#define NOT 0
#define VIS 1
#define COMP 2

ll stt[MAX];
ll mini[MAX];
vector<pll> edge[MAX];
priority_queue<pll, vector<pll>, greater<pll>> pq;

void dijkstra(ll start) {
  pll min;
  FOR(i, 1, n + 1) {
    stt[i] = NOT;
    mini[i] = INF;
  }
  mini[start] = 0;
  pq.push(make_pair(0, start));

  while (!pq.empty()) {
    min = pq.top();
    pq.pop();
    m = min.second;
    stt[m] = COMP;

    if (mini[m] < min.first)
      continue;

    FOR(i, 0, edge[m].size()) {
      k = edge[m][i].first;
      if (stt[k] != COMP) {
        if (edge[m][i].second + mini[m] < mini[k]) {
          mini[k] = edge[m][i].second + mini[m];
          stt[k] = VIS;
          pq.push(make_pair(mini[k], k));
        }
      }
    }
  }
}

int main(void) {
  ll a, b, c, q, x, y;
  cin >> n;

  FOR(i, 0, n - 1) {
    cin >> a >> b >> c;
    edge[a].push_back(make_pair(b, c));
    edge[b].push_back(make_pair(a, c));
  }

  cin >> q >> k;

  dijkstra(k);

  FOR(i, 0, q) {
    cin >> x >> y;
    cout << mini[x] + mini[y] << endl;
  }

  return 0;
}