Problem p00155 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

const double EPS = 1e-6;

struct Building {
  int x, y;
  Building() {
    x = -1;
    y = -1;
  }
};

struct Edge {
  int to;
  double cost;
  Edge() {}
  Edge(const int &_to, const double &_cost) {
    to = _to;
    cost = _cost;
  }
};

struct Data {
  int n;
  double cost;
  Data() {}
  Data(const int &_n, const double &_cost) {
    n = _n;
    cost = _cost;
  }
  bool operator<(const Data &a) const { return cost > a.cost; }
};

double GetDistance(const Building &b1, const Building &b2) {
  double dx = b1.x - b2.x, dy = b1.y - b2.y;
  return sqrt(dx * dx + dy * dy);
}

vector<Edge> e[1000];
Building v[1000];
priority_queue<Data> pq;

int main() {
  int N, M, B, S, G;

  while (cin >> N, N) {
    for (int i = 0; i < 1000; i++)
      e[i].clear();

    for (int i = 0; i < N; i++) {
      cin >> B;
      B--;
      cin >> v[B].x >> v[B].y;
    }

    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
        if (v[i].x == -1 || v[j].x == -1 || i == j)
          continue;
        double dist = GetDistance(v[i], v[j]);
        if (dist <= 50) {
          e[i].push_back(Edge(j, dist));
          e[j].push_back(Edge(i, dist));
        }
      }
    }

    cin >> M;
    for (int j = 0; j < M; j++) {
      cin >> S >> G;
      S--;
      G--;

      Data pq_top;
      pq.push(Data(S, 0));
      double memo[1000];
      fill_n(memo, 1000, -1);
      bool na = true;

      while (!pq.empty()) {
        pq_top = pq.top();
        pq.pop();

        if (memo[pq_top.n] > -0.5)
          continue;
        memo[pq_top.n] = pq_top.cost;
        if (pq_top.n == G) {
          na = false;
          break;
        }

        for (int i = 0; i < e[pq_top.n].size(); i++) {
          pq.push(Data(e[pq_top.n][i].to, pq_top.cost + e[pq_top.n][i].cost));
        }
      }
      while (!pq.empty())
        pq.pop();

      if (na) {
        puts("NA");
        continue;
      }

      stack<int> ans;
      while (true) {
        ans.push(G);
        if (G == S)
          break;
        for (int i = 0; i < e[G].size(); i++) {
          if (abs(memo[G] - memo[e[G][i].to] - e[G][i].cost) < EPS) {
            G = e[G][i].to;
            break;
          }
        }
      }

      printf("%d", ans.top() + 1);
      ans.pop();
      while (!ans.empty()) {
        printf(" %d", ans.top() + 1);
        ans.pop();
      }
      puts("");
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

const double EPS = 1e-6;

struct Building {
  int x, y;
  Building() {
    x = -1;
    y = -1;
  }
};

struct Edge {
  int to;
  double cost;
  Edge() {}
  Edge(const int &_to, const double &_cost) {
    to = _to;
    cost = _cost;
  }
};

struct Data {
  int n;
  double cost;
  Data() {}
  Data(const int &_n, const double &_cost) {
    n = _n;
    cost = _cost;
  }
  bool operator<(const Data &a) const { return cost > a.cost; }
};

double GetDistance(const Building &b1, const Building &b2) {
  double dx = b1.x - b2.x, dy = b1.y - b2.y;
  return sqrt(dx * dx + dy * dy);
}

vector<Edge> e[1000];
Building v[1000];
priority_queue<Data> pq;

int main() {
  int N, M, B, S, G;

  while (cin >> N, N) {
    for (int i = 0; i < 1000; i++)
      e[i].clear();

    for (int i = 0; i < N; i++) {
      cin >> B;
      B--;
      cin >> v[B].x >> v[B].y;
    }

    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
        if (v[i].x == -1 || v[j].x == -1 || i == j)
          continue;
        double dist = GetDistance(v[i], v[j]);
        if (dist <= 50) {
          e[i].push_back(Edge(j, dist));
          e[j].push_back(Edge(i, dist));
        }
      }
    }

    cin >> M;
    for (int j = 0; j < M; j++) {
      cin >> S >> G;
      S--;
      G--;

      Data pq_top;
      pq.push(Data(S, 0));
      double memo[1000];
      fill_n(memo, 1000, -1);
      bool na = true;

      while (!pq.empty()) {
        pq_top = pq.top();
        pq.pop();

        if (memo[pq_top.n] > -0.5)
          continue;
        memo[pq_top.n] = pq_top.cost;
        if (pq_top.n == G) {
          na = false;
          break;
        }

        for (int i = 0; i < e[pq_top.n].size(); i++) {
          pq.push(Data(e[pq_top.n][i].to, pq_top.cost + e[pq_top.n][i].cost));
        }
      }
      while (!pq.empty())
        pq.pop();

      if (na) {
        puts("NA");
        break;
      }

      stack<int> ans;
      while (true) {
        ans.push(G);
        if (G == S)
          break;
        for (int i = 0; i < e[G].size(); i++) {
          if (abs(memo[G] - memo[e[G][i].to] - e[G][i].cost) < EPS) {
            G = e[G][i].to;
            break;
          }
        }
      }

      printf("%d", ans.top() + 1);
      ans.pop();
      while (!ans.empty()) {
        printf(" %d", ans.top() + 1);
        ans.pop();
      }
      puts("");
    }
  }
  return 0;
}