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

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
#define int ll
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
double f(int x, int y) { return sqrt(x * x + y * y); }
signed main() {
  int n;
  while (cin >> n, n) {
    map<int, pii> mp;
    rep(i, n) {
      int hoge;
      pii foo;
      cin >> hoge >> foo.first >> foo.second;
      mp[--hoge] = foo;
    }
    vector<vector<pii>> edge(n, vector<pii>());
    for (auto itr = mp.begin(); itr != mp.end(); itr++) {
      for (auto comp = mp.begin(); comp != mp.end(); comp++) {
        if (itr == comp)
          continue;
        double dis = f(abs(itr->second.first - comp->second.first),
                       abs(itr->second.second - comp->second.second));
        if (dis <= 50) {
          // cout<<itr->first<<" "<<comp->first<<endl;
          edge[itr->first].push_back(pii(comp->first, dis));
        }
      }
    }
    int k;
    cin >> k;
    rep(_, k) {
      int s, g;
      cin >> s >> g;
      --s, --g;
      priority_queue<pii, vector<pii>, greater<pii>> que;
      vector<int> prev(n, INF);
      vector<int> d(n, INF);
      d[s] = 0;
      que.push(pii(0, s));
      while (!que.empty()) {
        pii now = que.top();
        que.pop();
        if (now.second == g)
          break;
        if (d[now.second] < now.first)
          continue;
        rep(i, edge[now.second].size()) {
          pii next = edge[now.second][i];
          if (d[next.first] < d[now.second] + next.second)
            continue;
          que.push(pii(d[now.second] + next.second, next.first));
          d[next.first] = now.first + next.second;
          prev[next.first] = now.second;
        }
      }
      if (edge[s].size() == 0 || edge[g].size() == 0 || d[g] == INF)
        cout << "NA" << endl;
      else {
        vi hoge;
        for (int v = g;; v = prev[v]) {
          hoge.push_back(v);
          if (v == s)
            break;
        }
        reverse(all(hoge));
        rep(i, hoge.size()) if (i) cout << " " << hoge[i] + 1;
        else cout << hoge[i] + 1;
        cout << endl;
      }
    }
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
#define int ll
typedef pair<int, int> pii;
int f(int x, int y) { return (x * x + y * y); }
signed main() {
  int n;
  while (cin >> n, n) {
    map<int, pii> mp;
    rep(i, n) {
      int hoge;
      pii foo;
      cin >> hoge >> foo.first >> foo.second;
      mp[--hoge] = foo;
    }
    vector<vector<pii>> edge(n + 1, vector<pii>());
    for (auto itr = mp.begin(); itr != mp.end(); itr++) {
      for (auto comp = mp.begin(); comp != mp.end(); comp++) {
        if (itr == comp)
          continue;
        double dis = f(abs(itr->second.first - comp->second.first),
                       abs(itr->second.second - comp->second.second));
        if (dis <= 2500) {
          // cout<<itr->first<<" "<<comp->first<<endl;
          edge[itr->first].push_back(pii(comp->first, dis));
        }
      }
    }
    int k;
    cin >> k;
    rep(_, k) {
      int s, g;
      cin >> s >> g;
      --s, --g;
      priority_queue<pii, vector<pii>, greater<pii>> que;
      vector<int> prev(n + 1, INF);
      vector<int> d(n + 1, INF);
      d[s] = 0;
      que.push(pii(0, s));
      while (!que.empty()) {
        pii now = que.top();
        que.pop();
        if (now.second == g)
          break;
        if (d[now.second] < now.first)
          continue;
        rep(i, edge[now.second].size()) {
          pii next = edge[now.second][i];
          if (d[next.first] < d[now.second] + next.second)
            continue;
          que.push(pii(d[now.second] + next.second, next.first));
          d[next.first] = now.first + next.second;
          prev[next.first] = now.second;
        }
      }
      if (edge[s].size() == 0 || edge[g].size() == 0)
        cout << "NA" << endl;
      else {
        vi hoge;
        for (int v = g;; v = prev[v]) {
          hoge.push_back(v);
          if (v == s)
            break;
        }
        reverse(all(hoge));
        rep(i, hoge.size()) if (i) cout << " " << hoge[i] + 1;
        else cout << hoge[i] + 1;
        cout << endl;
      }
    }
  }
}