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

#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef pair<int, int> P;
typedef pair<double, int> R;
typedef pair<P, R> Q;

int main() {
  int n, hx, hy, dx, dy;
  double r;
  while (cin >> n >> hx >> hy >> dx >> dy) {
    if (n == 0 && hx == 0 && hy == 0 && dx == 0 && dy == 0) {
      break;
    }
    hx -= dx;
    hy -= dy;
    vector<P> data;
    int x, y;
    for (int i = 0; i < n; i++) {
      cin >> x >> y;
      x -= dx;
      y -= dy;
      data.push_back(P(x, y));
    }
    vector<vector<double>> data2(n, vector<double>(n));
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        data2[i][j] = data2[j][i] =
            sqrt(pow(data[i].first - data[j].first, 2) +
                 pow(data[i].second - data[j].second, 2));
      }
    }
    vector<double> data3(n);
    for (int i = 0; i < n; i++) {
      data3[i] = sqrt(pow(data[i].first, 2) + pow(data[i].second, 2));
    }
    queue<Q> q;
    q.push(Q(P(hx, hy), R(0.0, 0)));
    bool hantei = false;
    while (!q.empty()) {
      Q p = q.front();
      q.pop();
      P xy = p.first;
      R limit = p.second;
      for (int i = 0; i < n; i++) {
        bool hantei2 = true;
        if ((limit.second & (1 << i)) == 0) {
          double dead = limit.first + sqrt(pow(data[i].first - xy.first, 2) +
                                           pow(data[i].second - xy.second, 2));
          if (data3[i] > dead) {
            int k = limit.second | (1 << i);
            if (k == (1 << n) - 1) {
              hantei = true;
              break;
            }
            for (int j = 0; j < n; j++) {
              if ((k & (1 << j)) == 0) {
                if (data3[j] <= dead + data2[i][j]) {
                  hantei2 = false;
                  break;
                }
              }
            }
            if (hantei2) {
              q.push(Q(data[i], R(dead, k)));
            }
          }
        }
      }
      if (hantei) {
        break;
      }
    }
    if (hantei) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef pair<int, int> P;
typedef pair<double, int> R;
typedef pair<P, R> Q;

int main() {
  int n, hx, hy, dx, dy;
  double r;
  while (cin >> n >> hx >> hy >> dx >> dy) {
    if (n == 0 && hx == 0 && hy == 0 && dx == 0 && dy == 0) {
      break;
    }
    hx -= dx;
    hy -= dy;
    vector<P> data;
    int x, y;
    for (int i = 0; i < n; i++) {
      cin >> x >> y;
      x -= dx;
      y -= dy;
      data.push_back(P(x, y));
    }
    queue<Q> q;
    q.push(Q(P(hx, hy), R(0.0, 0)));
    bool hantei = false;
    while (!q.empty()) {
      Q p = q.front();
      q.pop();
      P xy = p.first;
      R limit = p.second;
      for (int i = 0; i < n; i++) {
        bool hantei2 = true;
        if ((limit.second & (1 << i)) == 0) {
          double dead = limit.first + sqrt(pow(data[i].first - xy.first, 2) +
                                           pow(data[i].second - xy.second, 2));
          if (sqrt(pow(data[i].first, 2) + pow(data[i].second, 2)) <= dead) {
            hantei2 = false;
          }
          if (hantei2) {
            int k = limit.second | (1 << i);
            if (k == (1 << n) - 1) {
              hantei = true;
              break;
            }
            for (int j = 0; j < n; j++) {
              if ((k & (1 << j)) == 0) {
                if (sqrt(pow(data[j].first, 2) + pow(data[j].second, 2)) <=
                    dead + sqrt(pow(data[i].first - data[j].first, 2) +
                                pow(data[i].second - data[j].second, 2))) {
                  hantei2 = false;
                  break;
                }
              }
            }
            if (hantei2) {
              q.push(Q(data[i], R(dead, k)));
            }
          }
        }
      }
      if (hantei) {
        break;
      }
    }
    if (hantei) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  }
  return 0;
}