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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

int n;
double dis[32][32];
bool dfs(int unvisit, int cur, double time) {
  if (unvisit == 0)
    return true;

  for (int i = 0; i < n; ++i)
    if (unvisit >> i & 1 && time + dis[cur][i] > dis[n + 1][i])
      return false;

  for (int i = 0; i < n; ++i)
    if (unvisit >> i & 1 && time + dis[cur][i] < dis[n + 1][i])
      if (dfs(unvisit ^ 1 << i, i, time + dis[cur][i]))
        return true;

  return false;
}
int main() {
  int hx, hy, dx, dy;
  while (scanf("%d%d%d%d%d", &n, &hx, &hy, &dx, &dy), n) {
    int x[32], y[32];
    rep(i, n) scanf("%d%d", x + i, y + i);
    x[n] = hx, y[n] = hy;
    x[n + 1] = dx, y[n + 1] = dy;

    for (int i = 0; i <= n + 1; ++i)
      for (int j = 0; j <= n + 1; ++j)
        dis[i][j] = hypot(x[i] - x[j], y[i] - y[j]);

    puts(dfs((1 << n) - 1, n, 0) ? "YES" : "NO");
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

int n;
double dis[32][32];
bool dfs(int no_visit, int cur, double time) {
  if (no_visit == 0)
    return true;

  for (int i = 0; i < n; ++i)
    if (no_visit >> i & 1 && time > dis[n + 1][i])
      return false;

  for (int i = 0; i < n; ++i)
    if (no_visit >> i & 1 && time + dis[cur][i] < dis[n + 1][i])
      if (dfs(no_visit ^ 1 << i, i, time + dis[cur][i]))
        return true;

  return false;
}
int main() {
  double hx, hy, dx, dy;
  while (cin >> n >> hx >> hy >> dx >> dy, n) {
    double x[32], y[32];
    rep(i, n) cin >> x[i] >> y[i];
    x[n] = hx, y[n] = hy;
    x[n + 1] = dx, y[n + 1] = dy;

    for (int i = 0; i <= n + 1; ++i)
      for (int j = 0; j <= n + 1; ++j)
        dis[i][j] = hypot(x[i] - x[j], y[i] - y[j]);

    cout << (dfs((1 << n) - 1, n, 0) ? "YES" : "NO") << endl;
  }
}