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

#include <algorithm>
#include <bitset>
#include <cctype>
#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 <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VEC;
typedef vector<VEC> MAT;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;

// container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define MT(a, b, c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EPS 1e-8

double cost[22][22];
double tlimit[22];
int xx[22];
int cx[22], cy[22];
int n;

bool DFS(int pos, int vis, double dis) {
  if (vis == ((1 << (n + 1)) - 1))
    return true;
  // 枝狩り
  FOR(i, 1, n + 1)
  if ((vis & xx[i]) == 0 &&
      (dis + cost[pos][i] > tlimit[i] + EPS)) return false;
  FOR(i, 1, n + 1) {
    if ((vis & xx[i]) == 0 && (dis + cost[pos][i] + EPS < tlimit[i])) {
      if (DFS(i, vis | xx[i], dis + cost[pos][i])) {
        return true;
      }
    }
  }
  return false;
}

int main() {
  REP(i, 22) xx[i] = 1 << i;
  while (1) {
    int dx, dy;
    cin >> n >> cx[0] >> cy[0] >> dx >> dy;
    if (n == 0)
      return 0;
    FOR(i, 1, n + 1) cin >> cx[i] >> cy[i];
    REP(i, n + 1) {
      tlimit[i] = sqrt(pow(cx[i] * 1.0 - dx * 1.0, 2.0) +
                       pow(cy[i] * 1.0 - dy * 1.0, 2.0));
      REP(j, n + 1) {
        cost[i][j] = sqrt(pow(cx[i] * 1.0 - cx[j] * 1.0, 2.0) +
                          pow(cy[i] * 1.0 - cy[j] * 1.0, 2.0));
      }
    }
    if (DFS(0, 1, 0.0))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#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 <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VEC;
typedef vector<VEC> MAT;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;

// container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define MT(a, b, c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EPS 1e-8

double cost[22][22];
double tlimit[22];
int xx[22];
int cx[22], cy[22];
int n;

bool DFS(int pos, int vis, double dis) {
  if (vis == ((1 << (n + 1)) - 1))
    return true;
  // 枝狩り
  FOR(i, 1, n + 1)
  if ((vis & xx[i]) == 0 &&
      (dis + cost[pos][i] > tlimit[i] + EPS)) return false;
  FOR(i, 1, n + 1) {
    if ((vis & xx[i]) == 0) {
      if (DFS(i, vis | xx[i], dis + cost[pos][i])) {
        return true;
      }
    }
  }
  return false;
}

int main() {
  REP(i, 22) xx[i] = 1 << i;
  while (1) {
    int dx, dy;
    cin >> n >> cx[0] >> cy[0] >> dx >> dy;
    FOR(i, 1, n + 1) cin >> cx[i] >> cy[i];
    REP(i, n + 1) {
      tlimit[i] = sqrt(pow(cx[i] * 1.0 - dx * 1.0, 2.0) +
                       pow(cy[i] * 1.0 - dy * 1.0, 2.0));
      REP(j, n + 1) {
        cost[i][j] = sqrt(pow(cx[i] * 1.0 - cx[j] * 1.0, 2.0) +
                          pow(cy[i] * 1.0 - cy[j] * 1.0, 2.0));
      }
    }
    if (DFS(0, 1, 0.0))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}