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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
#define F first
#define S second
int N;
P H, D;
P C[22];
int K[22][22];
int dc[22];
int hc[22];
int deadK;

int getK(P a, P b) {
  return (a.F - b.F) * (a.F - b.F) + (a.S - b.S) * (a.S - b.S);
}

int sumK(int a, int b) { return a + b + 2 * sqrt(a) * sqrt(b); }

int solve(int id, int ky, int st) {
  if (st == (1 << N) - 1)
    return true;

  bool ret = true;
  int sum[22];
  for (int i = 0; i < N; i++) {
    if (st & (1 << i))
      continue;
    sum[i] = sumK(ky, K[id][i]);
    if (sum[i] >= dc[i])
      ret = false;
  }
  if (ret) {
    for (int i = 0; i < N; i++) {
      if (st & (1 << i))
        continue;
      if (sum[i] < dc[i])
        if (solve(i, sum[i], st | (1 << i)))
          return true;
    }
  }
  return false;
}

int main() {
  while (1) {
    cin >> N >> H.F >> H.S >> D.F >> D.S;
    if (!N && !H.F && !H.S && !D.F && !D.S)
      break;
    for (int i = 0; i < N; i++)
      cin >> C[i].F >> C[i].S;
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        K[i][j] = getK(C[i], C[j]);
    for (int i = 0; i < N; i++) {
      hc[i] = getK(C[i], H);
      dc[i] = getK(C[i], D);
    }
    bool res = true;
    for (int i = 0; i < N; i++) {
      if (hc[i] >= dc[i]) {
        res = false;
      }
    }
    if (res) {
      res = false;
      for (int i = 0; i < N; i++) {
        if (hc[i] < dc[i]) {
          if (solve(i, hc[i], (1 << i)))
            res = true;
        }
        if (res)
          break;
      }
    }
    if (res)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
#define F first
#define S second
int N;
P H, D;
P C[22];
int K[22][22];
int dc[22];
int hc[22];
int deadK;

int getK(P a, P b) {
  return (a.F - b.F) * (a.F - b.F) + (a.S - b.S) * (a.S - b.S);
}

int sumK(int a, int b) { return a + b + 2 * sqrt(a) * sqrt(b); }

int solve(int id, int ky, int st) {
  if (st == (1 << N) - 1)
    return true;
  for (int i = 0; i < N; i++) {
    if (st & (1 << i))
      continue;
    int sum = sumK(ky, K[id][i]);
    if (sum < deadK)
      if (solve(i, sum, st | (1 << i)))
        return true;
  }
  return false;
}

int main() {
  while (1) {
    cin >> N >> H.F >> H.S >> D.F >> D.S;
    if (!N && !H.F && !H.S && !D.F && !D.S)
      break;
    for (int i = 0; i < N; i++)
      cin >> C[i].F >> C[i].S;
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        K[i][j] = getK(C[i], C[j]);
    deadK = (1 << 29);
    for (int i = 0; i < N; i++) {
      hc[i] = getK(C[i], H);
      deadK = min(deadK, getK(C[i], D));
    }
    bool res = true;
    for (int i = 0; i < N; i++) {
      if (hc[i] >= deadK) {
        res = false;
      }
    }
    if (res) {
      res = false;
      for (int i = 0; i < N; i++) {
        if (hc[i] < deadK) {
          if (solve(i, hc[i], (1 << i)))
            res = true;
        }
        if (res)
          break;
      }
    }
    if (res)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}