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

#include <bits/stdc++.h>
using namespace std;

#define SQ(a) ((a) * (a))

struct Edge {
  int to;
  double cost;
  Edge(int to, double cost) : to(to), cost(cost) {}
};

struct State {
  int n;
  double sum;
  State(int n, int sum) : n(n), sum(sum) {}
  bool operator<(const State &t) const { return sum > t.sum; }
};

int x[1000], y[1000];
double d[1000];
int pre[1000];
vector<Edge> g[1000];

int btoi[1001];
int itob[1000];

vector<int> rec(int start, int goal) {
  vector<int> res;
  for (int e = goal; e != -1; e = pre[e]) {
    res.push_back(itob[e]);
  }
  reverse(res.begin(), res.end());
  if (res[0] != itob[start] || res[res.size() - 1] != itob[goal]) {
    return vector<int>();
  }
  return res;
}

void solve(int start, int goal) {
  fill(d, d + 1000, 1e50);
  memset(pre, -1, sizeof(pre));

  d[start] = 0.0;
  priority_queue<State> que;
  for (que.push(State(start, 0.0)); que.size(); que.pop()) {
    State st = que.top();
    if (d[st.n] < st.sum)
      continue;
    for (int v = 0; v < g[st.n].size(); v++) {
      Edge &e = g[st.n][v];
      if (d[e.to] < st.sum + e.cost)
        continue;
      d[e.to] = st.sum + e.cost;
      pre[e.to] = st.n;
      que.push(State(e.to, st.sum + e.cost));
    }
  }

  vector<int> route = rec(start, goal);
  if (route.size() == 0) {
    puts("NA");
    return;
  }
  for (int i = 0; i < route.size(); i++) {
    if (i)
      printf(" ");
    printf("%d", route[i]);
  }
  puts("");
}

int main() {
  int n;
  while (scanf("%d", &n), n) {
    for (int i = 0; i < n; i++)
      g[i].clear();
    for (int i = 0; i < n; i++) {
      int b;
      scanf("%d", &b);
      btoi[b] = i;
      itob[i] = b;
      scanf("%d %d", &x[i], &y[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          continue;
        double dist = sqrt(SQ(x[i] - x[j]) + SQ(y[i] - y[j]));
        if (dist > 50)
          continue;
        g[i].push_back(Edge(j, dist));
        g[j].push_back(Edge(i, dist));
      }
    }

    int m;
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
      int start, goal;
      scanf("%d %d", &start, &goal);
      solve(btoi[start], btoi[goal]);
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// #define double long double

const double INF = 1e+9;

struct Point {
  int x, y;

  Point(int a = 0, int b = 0) {
    x = a;
    y = b;
  }
};

struct Edge {
  int to;
  double cost;

  Edge(int a, double b) {
    to = a;
    cost = b;
  }
};

struct State {
  int pos;
  double sum;
  vector<int> route;

  State(int a, double b, vector<int> c) {
    pos = a;
    sum = b;
    route = c;
  }

  bool operator<(const State &t) const { return sum > t.sum; }
};

int norm(Point &a, Point &b) {
  return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}

int n;
Point b[2000];
double d[2000];
vector<int> v[2000];
vector<Edge> g[2000];

void search(int s) {
  for (int i = 0; i < 2000; i++) {
    v[i].clear();
    d[i] = INF;
  }

  priority_queue<State> que;
  que.push(State(s, 0.0, vector<int>(0)));
  for (; que.size(); que.pop()) {
    State st = que.top();
    if (d[st.pos] < st.sum)
      continue;

    d[st.pos] = st.sum;
    v[st.pos] = st.route;
    v[st.pos].push_back(st.pos);

    // cout << st.sum << endl;

    for (int i = 0; i < g[st.pos].size(); i++) {
      Edge &e = g[st.pos][i];
      if (d[e.to] < st.sum + e.cost)
        continue;
      que.push(State(e.to, st.sum + e.cost, v[st.pos]));
    }
  }
}

int main() {
  while (scanf("%d", &n), n) {
    for (int i = 0; i < 2000; i++) {
      g[i].clear();
    }

    for (int i = 0; i < n; i++) {
      int B, x, y;
      scanf("%d %d %d", &B, &x, &y);
      b[--B] = Point(x, y);
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          continue;

        if (norm(b[i], b[j]) <= 50 * 50) {
          g[i].push_back(Edge(j, sqrt(norm(b[i], b[j]))));
        }
      }
    }

    int m;
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
      int s, g;
      scanf("%d %d", &s, &g);
      search(--s);

      if (v[--g].size() == 0) {
        puts("NA");
        break;
      }

      for (int j = 0; j < v[g].size(); j++) {
        if (j)
          printf(" ");
        printf("%d", v[g][j] + 1);
      }
      puts("");
    }
  }

  return 0;
}