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

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999

pii d[2000];
bool used[2000];
double pass[2000][2000];
int n;

void dijk(int s) {
  rep(i, 2000) d[i] = pii(INF, -1);
  rep(i, 2000) used[i] = false;
  d[s] = pii(0, -1);

  while (1) {
    int v = -1;
    rep(u, n) {
      if (!used[u] && (v == -1 || d[u].first < d[v].first))
        v = u;
    }

    if (v == -1)
      break;

    used[v] = true;

    rep(u, n) {
      if (d[u].first > d[v].first + pass[v][u])
        d[u] = pii(d[v].first + pass[v][u], v);
    }
  }
}

int main() {
  while (cin >> n) {
    if (n == 0)
      break;

    rep(i, 2000) rep(j, 2000) pass[i][j] = INF;
    int b[2000], x[2000], y[2000];
    double res;
    rep(i, n) { cin >> b[i] >> x[i] >> y[i]; }
    rep(i, n) {
      rep(j, n) {
        if (i == j)
          pass[b[i] - 1][b[i] - 1] = 0;
        else if ((res = sqrt((x[i] - x[j]) * (x[i] - x[j]) +
                             (y[i] - y[j]) * (y[i] - y[j]))) <= 50 + 1e-9) {
          pass[b[i] - 1][b[j] - 1] = res;
          pass[b[j] - 1][b[i] - 1] = res;
        }
      }
    }
    int m;
    cin >> m;
    rep(i, m) {
      int s, g;
      cin >> s >> g;
      s--, g--;
      dijk(s);
      if (d[g].first == INF) {
        cout << "NA" << endl;
        continue;
      }
      int tmp = g;
      vector<int> ans;
      while (tmp != s) {
        ans.pb(tmp);
        tmp = d[tmp].second;
      }
      ans.pb(s);
      reverse(all(ans));
      rep(i, ans.size()) {
        if (i != 0)
          cout << " ";
        cout << ans[i] + 1;
      }
      cout << endl;
    }
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    int b[2000] = {}, x[2000] = {}, y[2000] = {};
    rep(i, n) {
      cin >> b[i] >> x[i] >> y[i];
      b[i]--;
    }

    ll data[2000][2000] = {};
    rep(i, n) rep(j, n) data[i][j] = 99999999;
    int prev[2000][2000] = {};

    rep(i, n) {
      rep(j, n) {
        ll res = pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2);
        if (res <= 2500) {
          data[i][j] = sqrt(res);
          prev[i][j] = i;
        }
      }
    }

    rep(k, n) {
      rep(i, n) {
        rep(j, n) {
          if (data[i][k] + data[k][j] < data[i][j]) {
            data[i][j] = data[i][k] + data[k][j];
            prev[i][j] = prev[k][j];
          }
          data[i][j] = min(data[i][k] + data[k][j], data[i][j]);
        }
      }
    }

    int m;
    cin >> m;

    int s, g;
    rep(i, m) {
      cin >> s >> g;
      s--, g--;
      if (data[s][g] != 99999999) {
        stack<int> ans;
        int pre = g;
        while (pre != s) {
          ans.push(pre);
          pre = prev[s][pre];
        }
        cout << s + 1;
        while (ans.size()) {
          cout << " " << ans.top() + 1;
          ans.pop();
        }
        cout << endl;
      } else
        cout << "NA" << endl;
    }
  }
  return 0;
}