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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
const int INF = 100000000;
// e_ÔÌ£
double dist[22][22];
// e_ÌÀW
vector<pair<int, int>> points;
int n, hx, hy, dx, dy;
bool ok = false;
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

// »ÝÌê©çßÄÂ\\ÈÅ¬ÊßbðÔ·
void dfs(int s, int cp, double passedSec) {
  if (ok)
    return;
  else if (s == 0) {
    ok = true;
    return;
  }
  for (int i = 0; i < n; i++) {
    // Ü¾rbgª1ÈçÎ
    if ((s >> i) & 1) {
      // áCªB·éOÉTõÂ\\©Ç¤©ð`FbN
      double dt = dist[cp][i];
      // ¤©çÚ®æÖÌ¼aðæ¾
      double r = dist[n + 1][i];
      // àµ¼aªz¦Ä¢½ç,}ëè
      if (dt + passedSec > r || EQ(dt + passedSec, r))
        return;
    }
  }

  for (int i = 0; i < n; i++) {
    // Ü¾rbgª1ÈçÎ
    if ((s >> i) & 1) {
      // áCªB·éOÉTõÂ\\©Ç¤©ð`FbN
      double dt = dist[cp][i];
      // ¤©çÚ®æÖÌ¼aðæ¾
      double r = dist[n + 1][i];
      dfs((s & (~(1 << i))), i, dt + passedSec);
    }
  }
}

int main() {
  while (cin >> n >> hx >> hy >> dx >> dy &&
         !(n == 0 && hx == 0 && hy == 0 && dx == 0 && dy == 0)) {
    points.clear();
    ok = false;
    // EÒÌúÊu
    for (int i = 0; i < n; i++) {
      pair<int, int> p;
      cin >> p.second >> p.first;
      points.push_back(p);
    }
    points.push_back(make_pair(hy, hx));
    points.push_back(make_pair(dy, dx));
    for (int i = 0; i < points.size(); i++) {
      for (int j = 0; j < points.size(); j++) {
        dist[i][j] = sqrt((double)(points[i].first - points[j].first) *
                              (points[i].first - points[j].first) +
                          (points[i].second - points[j].second) *
                              (points[i].second - points[j].second));
      }
    }
    dfs(((1 << n) - 1), n, 0.0);
    if (ok)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
const int INF = 100000000;
// e_ÔÌ£
double dist[22][22];
// e_ÌÀW
vector<pair<int, int>> points;
int n, hx, hy, dx, dy;
bool ok = false;
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))

// »ÝÌê©çßÄÂ\\ÈÅ¬ÊßbðÔ·
void dfs(int s, int cp, double passedSec) {
  if (ok)
    return;
  else if (s == 0) {
    ok = true;
    return;
  }
  for (int i = 0; i < n; i++) {
    // Ü¾rbgª1ÈçÎ
    if ((s >> i) & 1) {
      // áCªB·éOÉTõÂ\\©Ç¤©ð`FbN
      double dt = dist[cp][i];
      // ¤©çÚ®æÖÌ¼aðæ¾
      double r = dist[n + 1][i];
      // àµ¼aªz¦Ä¢½ç
      if (dt + passedSec > r || EQ(dt + passedSec, r))
        return;
      dfs((s & (~(1 << i))), i, dt + passedSec);
    }
  }
}

int main() {
  while (cin >> n >> hx >> hy >> dx >> dy &&
         !(n == 0 && hx == 0 && hy == 0 && dx == 0 && dy == 0)) {
    points.clear();
    ok = false;
    // EÒÌúÊu
    for (int i = 0; i < n; i++) {
      pair<int, int> p;
      cin >> p.second >> p.first;
      points.push_back(p);
    }
    points.push_back(make_pair(hy, hx));
    points.push_back(make_pair(dy, dx));

    for (int i = 0; i < points.size(); i++) {
      for (int j = 0; j < points.size(); j++) {
        dist[i][j] = sqrt((double)(points[i].first - points[j].first) *
                              (points[i].first - points[j].first) +
                          (points[i].second - points[j].second) *
                              (points[i].second - points[j].second));
      }
    }
    dfs(((1 << n) - 1), n, 0);
    if (ok)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }

  return 0;
}