Problem p00498 (C++) — buggy submission, verdict: Runtime Error

// Bokan ga bokka--nn!!
// Daily Lunch Special Tanoshii !!
// これは、頭が悪く競プロが世界で一番できないHIR180が
// IOI2014日本代表になるまでのN日間の記録である。
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define f first
#define s second
#define rep(i, x) for (int i = 0; i < x; i++)
int n, m, k, q;
vector<P> edge[100005];
vector<int> G[100005];
int up[20][100005];
int up2[20][100005];
int d[100005], dep[100005];
P1 e[200005];
class uf {
public:
  int par[100005], ran[100005];
  void init(int n) {
    for (int i = 1; i <= n; i++)
      par[i] = i, ran[i] = 0;
  }
  int find(int x) {
    if (par[x] == x)
      return par[x];
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (ran[x] < ran[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (ran[x] == ran[y])
        ran[y]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
} uf;
void dfs(int v, int u) {
  up[0][v] = u >= 1 ? min(d[u], d[v]) : INF;
  up2[0][v] = u;
  if (u >= 1)
    dep[v] = dep[u] + 1;
  for (int i = 0; i < G[v].size(); i++) {
    if (G[v][i] == u)
      continue;
    dfs(G[v][i], v);
  }
}
int calc(int u, int v) {
  if (dep[u] > dep[v])
    swap(u, v);
  int ret = INF;
  for (int i = 0; i < 20; i++) {
    if ((((dep[v] - dep[u]) >> i) & 1)) {
      ret =
          min(ret, up[i][v]); // printf("%d %d %d %d\\n",i,v,up[i][v],up2[i][v]);
      v = up2[i][v];          // cout << u <<" " << v  << endl;
    }
  }
  if (u == v)
    return ret;
  for (int i = 19; i >= 0; i--) {
    if (up2[i][u] != up2[i][v]) {
      ret = min(ret, min(up[i][u], up[i][v]));
      u = up2[i][u];
      v = up2[i][v];
    }
  }
  return min(ret, min(up[0][u], up[0][v]));
}
int main() {
  srand((unsigned int)time(NULL));
  scanf("%d%d%d%d", &n, &m, &k, &q);
  for (int i = 0; i < m; i++) {
    int x, y, v;
    scanf("%d%d%d", &x, &y, &v);
    edge[x].pb(mp(y, v));
    edge[y].pb(mp(x, v));
    e[i] = mp(0, mp(x, y));
  }
  fill(d + 1, d + n + 1, INF);
  priority_queue<P, vector<P>, greater<P>> que;
  for (int i = 0; i < k; i++) {
    int x;
    scanf("%d", &x);
    d[x] = 0;
    que.push(mp(0, x));
  }
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (p.f != d[p.s])
      continue;
    int val = p.f;
    int ver = p.s;
    for (int i = 0; i < edge[ver].size(); i++) {
      if (val + edge[ver][i].s < d[edge[ver][i].f]) {
        d[edge[ver][i].f] = val + edge[ver][i].s;
        que.push(mp(d[edge[ver][i].f], edge[ver][i].f));
      }
    }
  }
  for (int i = 0; i < m; i++) {
    e[i].f = min(d[e[i].s.f], d[e[i].s.s]);
  }
  sort(e, e + m, greater<P1>());
  uf.init(n);
  for (int i = 0; i < m; i++) {
    if (!uf.same(e[i].s.f, e[i].s.s)) {
      uf.unite(e[i].s.f, e[i].s.s);
      G[e[i].s.f].pb(e[i].s.s);
      G[e[i].s.s].pb(e[i].s.f);
    }
  }
  dep[1] = 0;
  dfs(1, -1);
  // for(int i=1;i<=n;i++) printf("%d %d %d\\n",up[0][i],up2[0][i],dep[i]);
  for (int i = 0; i < 19; i++) {
    for (int j = 1; j <= n; j++) {
      if (up2[i][j] == -1)
        up[i + 1][j] = INF;
      else
        up[i + 1][j] = min(up[i][up2[i][j]], up[i][j]);
      if (up2[i][j] == -1)
        up2[i + 1][j] = -1;
      else
        up2[i + 1][j] = up2[i][up2[i][j]];
    }
  }
  for (int i = 0; i < q; i++) {
    int xx, yy;
    scanf("%d%d", &xx, &yy);
    printf("%d\\n", calc(xx, yy));
  }
}

Error: -11

Fixed submission:

// Bokan ga bokka--nn!!
// Daily Lunch Special Tanoshii !!
// これは、頭が悪く競プロが世界で一番できないHIR180が
// IOI2014日本代表になるまでのN日間の記録である。
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 500000000
#define f first
#define s second
#define rep(i, x) for (int i = 0; i < x; i++)
int n, m, k, q;
vector<P> edge[100005];
vector<int> G[100005];
int up[20][100005];
int up2[20][100005];
int d[100005], dep[100005];
P1 e[200005];
class uf {
public:
  int par[100005], ran[100005];
  void init(int n) {
    for (int i = 1; i <= n; i++)
      par[i] = i, ran[i] = 0;
  }
  int find(int x) {
    if (par[x] == x)
      return par[x];
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (ran[x] < ran[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (ran[x] == ran[y])
        ran[y]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
} uf;
void dfs(int v, int u) {
  up[0][v] = u >= 1 ? min(d[u], d[v]) : INF;
  up2[0][v] = u;
  if (u >= 1)
    dep[v] = dep[u] + 1;
  for (int i = 0; i < G[v].size(); i++) {
    if (G[v][i] == u)
      continue;
    dfs(G[v][i], v);
  }
}
int calc(int u, int v) {
  if (dep[u] > dep[v])
    swap(u, v);
  int ret = INF;
  for (int i = 0; i < 20; i++) {
    if ((((dep[v] - dep[u]) >> i) & 1)) {
      ret = min(ret, up[i][v]);
      v = up2[i][v];
    }
  }
  if (u == v)
    return ret;
  for (int i = 19; i >= 0; i--) {
    if (up2[i][u] != up2[i][v]) {
      u = up2[i][u];
      v = up2[i][v];
      ret = min(ret, min(up[i][u], up[i][v]));
    }
  }
  return min(ret, min(up[0][u], up[0][v]));
}
int main() {
  srand((unsigned int)time(NULL));
  scanf("%d%d%d%d", &n, &m, &k, &q);
  for (int i = 0; i < m; i++) {
    int x, y, v;
    scanf("%d%d%d", &x, &y, &v);
    edge[x].pb(mp(y, v));
    edge[y].pb(mp(x, v));
    e[i] = mp(0, mp(x, y));
  }
  fill(d + 1, d + n + 1, INF);
  priority_queue<P, vector<P>, greater<P>> que;
  for (int i = 0; i < k; i++) {
    int x;
    scanf("%d", &x);
    d[x] = 0;
    que.push(mp(0, x));
  }
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (p.f != d[p.s])
      continue;
    int val = p.f;
    int ver = p.s;
    for (int i = 0; i < edge[ver].size(); i++) {
      if (val + edge[ver][i].s < d[edge[ver][i].f]) {
        d[edge[ver][i].f] = val + edge[ver][i].s;
        que.push(mp(d[edge[ver][i].f], edge[ver][i].f));
      }
    }
  }
  for (int i = 0; i < m; i++) {
    e[i].f = min(d[e[i].s.f], d[e[i].s.s]);
  }
  sort(e, e + m);
  uf.init(n);
  for (int i = 0; i < m; i++) {
    if (!uf.same(e[i].s.f, e[i].s.s)) {
      uf.unite(e[i].s.f, e[i].s.s);
      G[e[i].s.f].pb(e[i].s.s);
      G[e[i].s.s].pb(e[i].s.f);
    }
  }
  dep[1] = 0;
  dfs(1, -1);
  for (int i = 0; i < 19; i++) {
    for (int j = 1; j <= n; j++) {
      if (up[i][j] == INF)
        up[i + 1][j] = INF;
      up[i + 1][j] = min(up[i][up[i][j]], up[i][j]);
      if (up2[i][j] == -1)
        up2[i + 1][j] = -1;
      up2[i + 1][j] = up2[i][up2[i][j]];
    }
  }
  for (int i = 0; i < q; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    printf("%d\\n", calc(x, y));
  }
}