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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;

#define INF (1 << 20)
#define MAX_V 200

int path[MAX_V];
int cost[MAX_V][MAX_V];
bool used[MAX_V];
int d[MAX_V];

// ?????????O(V^2)

void shortest_path(int s, int V) { // V???????????°
  for (int i = 0; i < V; i++) {
    d[i] = INF;
    used[i] = false;
  }
  d[s] = 0;
  path[s] = -1;

  while (true) {

    int add_v = -1;

    for (int u = 0; u < V; u++) {
      if (!used[u] && (add_v == -1 || d[u] < d[add_v])) {
        add_v = u;
      }
    } // ??´??°????????????????±????
    // cout << add_v << " ";
    if (add_v == -1) {
      break;
    } // ??´??°???????????????????????´????????????
    used[add_v] = true;

    for (int u = 0; u < V; u++) {
      if (d[add_v] + cost[add_v][u] < d[u]) {
        d[u] = d[add_v] + cost[add_v][u];
        path[u] = add_v;
      }
    } // ???????????????????????????????°???????????????´??°
  }
}

void path_track2(int num) {
  if (num == -1) {
    return;
  }
  path_track2(path[num]);
  cout << num + 1 << " ";
}

void path_track(int num) {
  if (num == -1) {
    cout << endl;
    return;
  }
  path_track2(path[num]);
  cout << num + 1 << endl;
}

int main() {

  int V, m;

  while (true) {

    cin >> V;

    if (V == 0) {
      break;
    }

    double pos_x[200], pos_y[200];

    for (int i = 0; i < V; i++) {
      int b;
      double x, y;
      cin >> b >> x >> y;
      pos_x[b - 1] = x;
      pos_y[b - 1] = y;
      path[b - 1] = -1;
    }

    for (int i = 0; i < V; i++) {
      for (int j = 0; j < V; j++) {
        if (i == j) {
          cost[i][j] = INF;
        } else if ((pos_x[i] - pos_x[j]) * (pos_x[i] - pos_x[j]) +
                       (pos_y[i] - pos_y[j]) * (pos_y[i] - pos_y[j]) <=
                   2500) {
          cost[i][j] = sqrt((pos_x[i] - pos_x[j]) * (pos_x[i] - pos_x[j]) +
                            (pos_y[i] - pos_y[j]) * (pos_y[i] - pos_y[j]));
        } else {
          cost[i][j] = INF;
        }
      }
    }

    cin >> m;

    for (int loop = 0; loop < m; loop++) {
      int s, g;
      cin >> s >> g;
      s--;
      g--;

      shortest_path(s, V);

      if (d[g] >= INF) {
        cout << "NA" << endl;
      } else {
        path_track(g);
      }
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;

#define INF (1 << 20)
#define MAX_V 200

int path[MAX_V];
int cost[MAX_V][MAX_V];
bool used[MAX_V];
int d[MAX_V];

// ?????????O(V^2)

void shortest_path(int s, int V) { // V???????????°
  for (int i = 0; i < V; i++) {
    d[i] = INF;
    used[i] = false;
  }
  d[s] = 0;

  while (true) {

    int add_v = -1;

    for (int u = 0; u < V; u++) {
      if (!used[u] && (add_v == -1 || d[u] < d[add_v])) {
        add_v = u;
      }
    } // ??´??°????????????????±????
    // cout << add_v << " ";
    if (add_v == -1) {
      break;
    } // ??´??°???????????????????????´????????????
    used[add_v] = true;

    for (int u = 0; u < V; u++) {
      if (d[add_v] + cost[add_v][u] < d[u]) {
        d[u] = d[add_v] + cost[add_v][u];
        path[u] = add_v;
      }
    } // ???????????????????????????????°???????????????´??°
  }
}

void path_track2(int num) {
  if (num == -1) {
    return;
  }
  path_track2(path[num]);
  cout << num + 1 << " ";
}

void path_track(int num) {
  if (num == -1) {
    cout << endl;
    return;
  }
  path_track2(path[num]);
  cout << num + 1 << endl;
  ;
}

int main() {

  int V, m;

  while (true) {

    cin >> V;

    if (V == 0) {
      break;
    }

    double pos_x[200], pos_y[200];

    for (int i = 0; i < V; i++) {
      int b;
      double x, y;
      cin >> b >> x >> y;
      pos_x[b - 1] = x;
      pos_y[b - 1] = y;
      path[b - 1] = -1;
    }

    for (int i = 0; i < V; i++) {
      for (int j = 0; j < V; j++) {
        if (i == j) {
          cost[i][j] = INF;
        } else if ((pos_x[i] - pos_x[j]) * (pos_x[i] - pos_x[j]) +
                       (pos_y[i] - pos_y[j]) * (pos_y[i] - pos_y[j]) <=
                   2500) {
          cost[i][j] = sqrt((pos_x[i] - pos_x[j]) * (pos_x[i] - pos_x[j]) +
                            (pos_y[i] - pos_y[j]) * (pos_y[i] - pos_y[j]));
        } else {
          cost[i][j] = INF;
        }
      }
    }

    cin >> m;

    for (int loop = 0; loop < m; loop++) {
      int s, g;
      cin >> s >> g;
      s--;
      g--;
      shortest_path(s, V);

      if (d[g] >= INF) {
        cout << "NA" << endl;
      } else {
        path_track(g);
        cout << endl;
      }
    }
  }
  return 0;
}