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

#include <bits/stdc++.h>
using namespace std;

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

struct Point {
  double x, y;
  Point() {}
  Point(double x, double y) : x(x), y(y) {}
};
Point operator+(Point a, Point b) { return Point(a.x + b.x, a.y + b.y); }
Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); }
double abs(Point p) { return sqrt(p.x * p.x + p.y * p.y); }

int dfs(double t, Point cur, Point devil, const vector<Point> &cs, int flg) {
  int n = cs.size();
  rep(i, n) if (!(flg >> i & 1)) {
    double spend = abs(cs[i] - cur);
    if (abs(cs[i] - devil) < t + spend + EPS)
      return 0;
  }
  int res = 0;
  rep(i, n) if (!(flg >> i & 1)) {
    double spend = abs(cs[i] - cur);
    res = max(res, 1 + dfs(t + spend, cs[i], devil, cs, flg | 1 << i));
  }
  return res;
}

int main() {
  for (int n; cin >> n && n;) {
    Point h, d;
    cin >> h.x >> h.y >> d.x >> d.y;
    vector<Point> cs(n);
    for (auto &c : cs)
      cin >> c.x >> c.y;
    cout << (dfs(0, h, d, cs, 0) == n ? "YES" : "NO") << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

struct Point {
  double x, y;
  Point() {}
  Point(double x, double y) : x(x), y(y) {}
};
Point operator+(Point a, Point b) { return Point(a.x + b.x, a.y + b.y); }
Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); }
double abs(Point p) { return sqrt(p.x * p.x + p.y * p.y); }

int dfs(double t, Point cur, Point devil, const vector<Point> &cs, int flg) {
  int n = cs.size(), res = 0;
  rep(i, n) if (!(flg >> i & 1)) {
    double spend = abs(cs[i] - cur);
    if (abs(cs[i] - devil) < t + spend + EPS)
      return 0;
    res = max(res, 1 + dfs(t + spend, cs[i], devil, cs, flg | 1 << i));
  }
  return res;
}

int main() {
  for (int n; cin >> n && n;) {
    Point h, d;
    cin >> h.x >> h.y >> d.x >> d.y;
    vector<Point> cs(n);
    for (auto &c : cs)
      cin >> c.x >> c.y;
    cout << (dfs(0, h, d, cs, 0) == n ? "YES" : "NO") << endl;
  }
}