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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define all(n) n.begin(), n.end()
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define S second
#define ppins(a, b, c, d) PP(P(a, b), P(c, d))
#define pb push_back
#define pf push_front
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef long long ll;
typedef unsigned long long ull;
int n, hx, hy, dx, dy;
vector<P> vec;
int LIM;
bool Found;
const double EPS = 1e-10;

void print(int used) {
  rep(i, n) {
    if (used >> i & 1)
      cout << "1";
    else
      cout << "0";
  }
  cout << endl;
}

bool Input() {
  cin >> n >> hx >> hy >> dx >> dy;
  if (n + hx + hy + dx + dy == 0)
    return true;

  vec.clear(), LIM = 0, Found = false;
  rep(i, n) {
    int X, Y;
    cin >> X >> Y;
    vec.pb(P(X, Y));
    LIM = LIM | (1 << i);
  }
  return false;
}

bool isCan(P my, double ene, P p, int used) {

  double cost = sqrt(pow(p.F - my.F, 2) + pow(p.S - my.S, 2));

  rep(i, n) {
    if ((used >> i) & 1)
      continue;

    double dist = sqrt(pow(vec[i].F - dx, 2) + pow(vec[i].S - dy, 2));

    // cout << "circle : " << ene+cost << " , dist : " << dist << endl;
    if (ene + cost >= dist)
      return false;
  }
  return true;
}

bool check(int used, P my, double cost) {
  rep(i, n) {
    if (used >> i & 1)
      continue;
    if (hypot(vec[i].F - dx, vec[i].S - dy) -
            (hypot(vec[i].F - my.F, vec[i].S - my.S) + cost) <
        EPS) {
      return false;
    }
  }
  return true;
}

void rec(P my, double cir, int used) {
  if (Found)
    return;

  if (used == LIM) {
    Found = true;
    return;
  }

  if (!check(used, my, cir))
    return;

  rep(i, n) {
    if ((used >> i) & 1)
      continue;
    rec(vec[i], cir + hypot(vec[i].F - my.F, vec[i].S - my.S), used | (1 << i));
  }
}

int main() {
  while (true) {
    if (Input())
      break;

    rec(P(hx, hy), 0, 0);
    Found ? cout << "YES" << endl : cout << "NO" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define all(n) n.begin(), n.end()
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define S second
#define ppins(a, b, c, d) PP(P(a, b), P(c, d))
#define pb push_back
#define pf push_front
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef long long ll;
typedef unsigned long long ull;
int n, hx, hy, dx, dy;
vector<P> vec;
int LIM;
bool Found;
const double EPS = (1 >> 10);

void print(int used) {
  rep(i, n) {
    if (used >> i & 1)
      cout << "1";
    else
      cout << "0";
  }
  cout << endl;
}

bool Input() {
  cin >> n >> hx >> hy >> dx >> dy;
  if (n + hx + hy + dx + dy == 0)
    return true;

  vec.clear(), LIM = 0, Found = false;
  rep(i, n) {
    int X, Y;
    cin >> X >> Y;
    vec.pb(P(X, Y));
    LIM = LIM | (1 << i);
  }
  return false;
}

bool isCan(P my, double ene, P p, int used) {

  double cost = sqrt(pow(p.F - my.F, 2) + pow(p.S - my.S, 2));

  rep(i, n) {
    if ((used >> i) & 1)
      continue;

    double dist = sqrt(pow(vec[i].F - dx, 2) + pow(vec[i].S - dy, 2));

    // cout << "circle : " << ene+cost << " , dist : " << dist << endl;
    if (ene + cost >= dist)
      return false;
  }
  return true;
}

void rec(P my, double cir, int used) {
  if (Found)
    return;

  // print(used);

  if (used == LIM) {
    Found = true;
    return;
  }

  rep(i, n) {
    if ((used >> i) & 1)
      continue;
    if (isCan(my, cir, vec[i], used)) {
      rec(vec[i], cir + sqrt(pow(vec[i].F - my.F, 2) + pow(vec[i].S - my.S, 2)),
          (used | 1 << i));
    }
  }
}

int main() {
  while (true) {
    if (Input())
      break;

    rec(P(hx, hy), 0, 0);
    Found ? cout << "YES" << endl : cout << "NO" << endl;
  }
  return 0;
}