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

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
double lim[20], limbit[1 << 20], inf = 1e9, eps = 1e-9, limit[21][21];
int n, hx, hy, mx, my, cx[21], cy[21];
void calc() {
  rep(i, n + 1) rep(j, n + 1) {
    if (i == j)
      limit[i][j] = 0;
    double sx = cx[i], sy = cy[i], tx = cx[j], ty = cy[j],
           d = sqrt((tx - sx) * (tx - sx) + (ty - sy) * (ty - sy)),
           dx = (tx - sx) / d, dy = (ty - sy) / d, x = sx - mx, y = sy - my;
    double b = 2 * d, c = -(x * x + y * y + 2 * d * (x * dx + y * dy));
    limit[i][j] = (-b + sqrt(b * b - 4 * c)) / 2;
  }
}
bool dfs(int bit, int v, double t) {
  if (bit == (1 << n) - 1)
    return true;
  if (t + eps > limbit[bit])
    return false;
  rep(i, n) {
    if ((bit >> i) & 1)
      continue;
    if (t + eps > limit[v][i])
      return false;
  }
  rep(i, n) {
    if ((bit >> i) & 1)
      continue;
    if (dfs(bit + (1 << i), i,
            t + sqrt((cx[i] - cx[v]) * (cx[i] - cx[v]) +
                     (cy[i] - cy[v]) * (cy[i] - cy[v]))))
      return true;
  }
  return false;
}
int main() {
  while (true) {
    cin >> n;
    cin >> cx[n] >> cy[n] >> mx >> my;
    if (n == 0)
      break;
    rep(i, n) {
      cin >> cx[i] >> cy[i];
      lim[i] = sqrt((cx[i] - mx) * (cx[i] - mx) + (cy[i] - my) * (cy[i] - my));
    }
    rep(i, 1 << n) {
      limbit[i] = inf;
      rep(j, n) {
        if ((i << j) & 1 == 0)
          limbit[i] = min(limbit[i], lim[j]);
      }
    }
    calc();
    cout << (dfs(0, n, 0) ? "YES\\n" : "NO\\n");
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
double lim[20], limbit[1 << 20], inf = 1e9, eps = 1e-9, limit[21][21];
int n, hx, hy, mx, my, cx[21], cy[21];
void calc() {
  rep(i, n + 1) rep(j, n + 1) {
    if (i == j)
      limit[i][j] = 0;
    double sx = cx[i], sy = cy[i], tx = cx[j], ty = cy[j],
           d = sqrt((tx - sx) * (tx - sx) + (ty - sy) * (ty - sy)),
           dx = (tx - sx) / d, dy = (ty - sy) / d, x = sx - mx, y = sy - my;
    double b = 2 * d, c = -(x * x + y * y + 2 * d * (x * dx + y * dy));
    limit[i][j] = (-b + sqrt(b * b - 4 * c)) / 2;
  }
}
bool dfs(int bit, int v, double t) {
  if (bit == (1 << n) - 1)
    return true;
  if (t + eps > limbit[bit])
    return false;
  rep(i, n) {
    if ((bit >> i) & 1)
      continue;
    if (t + eps > limit[v][i])
      return false;
    if (dfs(bit + (1 << i), i,
            t + sqrt((cx[i] - cx[v]) * (cx[i] - cx[v]) +
                     (cy[i] - cy[v]) * (cy[i] - cy[v]))))
      return true;
  }
  return false;
}
int main() {
  while (true) {
    scanf("%d%d%d%d%d", &n, &cx[n], &cy[n], &mx, &my);
    if (n == 0)
      break;
    rep(i, n) {
      scanf("%d%d", &cx[i], &cy[i]);
      lim[i] = sqrt((cx[i] - mx) * (cx[i] - mx) + (cy[i] - my) * (cy[i] - my));
    }
    rep(i, 1 << n) {
      limbit[i] = inf;
      rep(j, n) {
        if ((i << j) & 1 == 0)
          limbit[i] = min(limbit[i], lim[j]);
      }
    }
    calc();
    cout << (dfs(0, n, 0) ? "YES\\n" : "NO\\n");
  }
}