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

#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<double, int> P;
priority_queue<P, vector<P>, greater<P>> Q;
#define MAX_N 1005
int x[MAX_N], y[MAX_N];
double dist[MAX_N];
vector<int> path[MAX_N];
vector<P> X[MAX_N];
void dijkstra(int a, int b) {
  memset(dist, 1e+9, sizeof(dist));
  for (int i = 0; i < MAX_N; i++) {
    path[i].clear();
    dist[i] = 1e+9;
  }
  Q.push(make_pair(0.0, a));
  dist[a] = 0;
  path[a].push_back(a);
  while (!Q.empty()) {
    pair<double, int> pa = Q.top();
    Q.pop();
    double a1 = pa.first;
    int a2 = pa.second;
    for (int i = 0; i < X[a2].size(); i++) {
      int to = X[a2][i].second;
      double dis = X[a2][i].first;
      if (dist[to] > a1 + dis) {
        dist[to] = a1 + dis;
        path[to].clear();
        path[to] = path[a2];
        path[to].push_back(to);
        Q.push(make_pair(dist[to], to));
      }
    }
  }
  if (path[b].size() == 0) {
    cout << "NA" << endl;
  } else {
    for (int i = 0; i < path[b].size(); i++) {
      if (i) {
        cout << ' ';
      }
      cout << path[b][i];
    }
    cout << endl;
  }
}
int main() {
  int N, Q, s, g, a, b, c;
  while (true) {
    for (int i = 0; i < MAX_N; i++) {
      X[i].clear();
    }
    cin >> N;
    if (N == 0) {
      break;
    }
    for (int i = 0; i < N; i++) {
      cin >> a >> b >> c;
      x[a] = b;
      y[a] = c;
    }
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j <= N; j++) {
        double dist2 = sqrtl((x[i] - x[j]) * (x[i] - x[j]) +
                             (y[i] - y[j]) * (y[i] - y[j]));
        if (dist2 <= 50.0) {
          X[i].push_back(make_pair(dist2, j));
        }
      }
    }
    cin >> Q;
    for (int i = 0; i < Q; i++) {
      cin >> s >> g;
      dijkstra(s, g);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<double, int> P;
priority_queue<P, vector<P>, greater<P>> Q;
#define MAX_N 1005
int x[MAX_N], y[MAX_N];
double dist[MAX_N];
vector<int> path[MAX_N];
vector<P> X[MAX_N];
void dijkstra(int a, int b) {
  memset(dist, 1e+9, sizeof(dist));
  for (int i = 0; i < MAX_N; i++) {
    path[i].clear();
  }
  Q.push(make_pair(0.0, a));
  dist[a] = 0;
  path[a].push_back(a);
  while (!Q.empty()) {
    pair<double, int> pa = Q.top();
    Q.pop();
    double a1 = pa.first;
    int a2 = pa.second;
    for (int i = 0; i < X[a2].size(); i++) {
      int to = X[a2][i].second;
      double dis = X[a2][i].first;
      if (dist[to] > a1 + dis) {
        dist[to] = a1 + dis;
        path[to].clear();
        path[to] = path[a2];
        path[to].push_back(to);
        Q.push(make_pair(dist[to], to));
      }
    }
  }
  if (path[b].size() == 0) {
    cout << "NA" << endl;
  } else {
    for (int i = 0; i < path[b].size(); i++) {
      if (i) {
        cout << ' ';
      }
      cout << path[b][i];
    }
    cout << endl;
  }
}
int main() {
  int N, Q, s, g;
  while (true) {
    cin >> N;
    if (N == 0) {
      break;
    }
    for (int i = 0; i < N; i++) {
      cin >> x[i] >> y[i];
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        double dist2 =
            (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]);
        if (dist2 <= 50.0) {
          X[i].push_back(make_pair(dist2, j));
        }
      }
    }
    cin >> Q;
    for (int i = 0; i < Q; i++) {
      cin >> s >> g;
      dijkstra(s, g);
    }
  }
  return 0;
}