Problem p00155 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <math.h>
#include <queue>
#include <vector>

using namespace std;

const int MAX = 1000;

struct int2 {
  int x;
  int y;
};

class Node {
public:
  int id;
  double cost;
  int parent;
  bool visited;
  vector<int> edge_to;
  vector<double> edge_cost;

  Node() {}
  Node(int id) : id(id), cost(-1.0), parent(-1), visited(false) {}

  bool operator<(const Node &n) const { return cost > n.cost; }
};

int n, m;
Node nodes[MAX];
int2 pos[MAX];
int sid, gid;

void solve() {
  priority_queue<Node> q;
  nodes[sid].cost = 0.0;
  q.push(nodes[sid]);

  while (!q.empty()) {
    Node nn = q.top();
    q.pop();

    if (nn.visited)
      continue;
    if (nn.id == gid)
      return;
    nn.visited = true;
    for (int i = 0; i < nn.edge_to.size(); i++) {
      int to = nn.edge_to[i];
      double cost = nn.cost + nn.edge_cost[i];
      if (nodes[to].cost > cost || nodes[to].cost < 0.0) {
        if (nodes[to].visited)
          continue;
        nodes[to].cost = cost;
        nodes[to].parent = nn.id;
        q.push(nodes[to]);
      }
    }
  }
}

void reset() {
  for (int i = 0; i < n; i++) {
    nodes[i].cost = -1;
    nodes[i].parent = -1;
    nodes[i].visited = false;
  }
}

int main(int argc, char **argv) {
  int b, x, y;
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      cin >> b >> x >> y;
      int2 tmp = {x, y};
      pos[b - 1] = tmp;
      nodes[i] = Node(i);
    }

    for (int i = 1; i < n; i++) {
      for (int j = 0; j < i; j++) {
        int dx = pos[i].x - pos[j].x;
        int dy = pos[i].y - pos[j].y;
        double dist = sqrt((double)(dx * dx + dy * dy));
        if (dist <= 50.0) {
          nodes[i].edge_to.push_back(j);
          nodes[i].edge_cost.push_back(dist);
          nodes[j].edge_to.push_back(i);
          nodes[j].edge_cost.push_back(dist);
        }
      }
    }

    cin >> m;

    for (int k = 0; k < m; k++) {
      cin >> sid >> gid;
      sid = sid - 1;
      gid = gid - 1;

      solve();

      vector<int> way;
      way.push_back(gid);
      Node nn = nodes[gid];
      while (nn.parent != -1) {
        way.push_back(nn.parent);
        nn = nodes[nn.parent];
      }

      if (way.size() > 1) {
        for (int i = 0; i < way.size(); i++) {
          if (i != 0)
            cout << " ";
          cout << way[way.size() - i - 1] + 1;
        }
      } else {
        cout << "NA";
      }
      cout << endl;
    }
  }
}

Fixed submission:
#include <iostream>
#include <math.h>
#include <queue>
#include <vector>

using namespace std;

const int MAX = 1000;

struct int2 {
  int x;
  int y;
};

class Node {
public:
  int id;
  double cost;
  int parent;
  bool visited;
  vector<int> edge_to;
  vector<double> edge_cost;

  Node() {}
  Node(int id) : id(id), cost(-1.0), parent(-1), visited(false) {}

  bool operator<(const Node &n) const { return cost > n.cost; }
};

int n, m;
Node nodes[MAX];
int2 pos[MAX];
int sid, gid;

void solve() {
  priority_queue<Node> q;
  nodes[sid].cost = 0.0;
  q.push(nodes[sid]);

  while (!q.empty()) {
    Node nn = q.top();
    q.pop();

    if (nn.visited)
      continue;
    if (nn.id == gid)
      return;
    nn.visited = true;
    for (int i = 0; i < nn.edge_to.size(); i++) {
      int to = nn.edge_to[i];
      double cost = nn.cost + nn.edge_cost[i];
      if (nodes[to].cost > cost || nodes[to].cost < 0.0) {
        if (nodes[to].visited)
          continue;
        nodes[to].cost = cost;
        nodes[to].parent = nn.id;
        q.push(nodes[to]);
      }
    }
  }
}

void reset() {
  for (int i = 0; i < n; i++) {
    nodes[i].cost = -1;
    nodes[i].parent = -1;
    nodes[i].visited = false;
  }
}

int main(int argc, char **argv) {
  int b, x, y;
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      cin >> b >> x >> y;
      int2 tmp = {x, y};
      pos[b - 1] = tmp;
      nodes[i] = Node(i);
    }

    for (int i = 1; i < n; i++) {
      for (int j = 0; j < i; j++) {
        int dx = pos[i].x - pos[j].x;
        int dy = pos[i].y - pos[j].y;
        double dist = sqrt((double)(dx * dx + dy * dy));
        if (dist <= 50.0) {
          nodes[i].edge_to.push_back(j);
          nodes[i].edge_cost.push_back(dist);
          nodes[j].edge_to.push_back(i);
          nodes[j].edge_cost.push_back(dist);
        }
      }
    }

    cin >> m;

    for (int k = 0; k < m; k++) {
      if (k)
        reset();
      cin >> sid >> gid;
      sid = sid - 1;
      gid = gid - 1;

      solve();

      vector<int> way;
      way.push_back(gid);
      Node nn = nodes[gid];
      while (nn.parent != -1) {
        way.push_back(nn.parent);
        nn = nodes[nn.parent];
      }

      if (way.size() > 1) {
        for (int i = 0; i < way.size(); i++) {
          if (i != 0)
            cout << " ";
          cout << way[way.size() - i - 1] + 1;
        }
      } else {
        cout << "NA";
      }
      cout << endl;
    }
  }
}