Problem p01133 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

typedef complex<double> P;

P syoki, maou;
P houseki[20];
int n;
vector<bool> v;

bool rec(int now, double time) {
  // REP(i,n) {
  //   cout << v[i];
  // }cout << endl;
  // printf("now=%d, time=%f\\n",now,time);
  bool ff = 0;
  REP(i, n) {
    if (!v[i]) {
      //      cout << i << " " << abs(maou-houseki[i]) << ", " <<
      //      time+abs(houseki[i]-houseki[now]) << endl;
      if (abs(maou - houseki[i]) <= time + abs(houseki[i] - houseki[now])) {
        return false;
      }
      ff = 1;
    }
  }
  if (!ff) { // ツ全ツ閉板づづつス
    return true;
  }

  REP(i, n) {
    if (!v[i]) {
      v[i] = 1;
      if (rec(i, time + abs(houseki[i] - houseki[now])))
        return true;
      v[i] = 0;
      ff = 1;
    }
  }
}

int main() {
  int hx, hy, dx, dy;
  while (cin >> n >> hx >> hy >> dx >> dy, n || hx || hy || dx || dy) {
    syoki = P(hx, hy);
    maou = P(dx, dy);
    REP(i, n) {
      double x, y;
      cin >> x >> y;
      houseki[i] = P(x, y);
    }
    v = vector<bool>(n);
    bool f = 0;
    REP(i, n) {
      v[i] = 1;
      if (abs(houseki[i] - syoki) < abs(houseki[i] - maou))
        if (rec(i, abs(houseki[i] - syoki))) {
          f = 1;
          break;
        }
      v[i] = 0;
    }
    if (f)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

typedef complex<double> P;

int main() {
  int n, hx, hy, dx, dy;
  while (cin >> n >> hx >> hy >> dx >> dy, n || hx || hy || dx || dy) {
    P syoki = P(hx, hy);
    P maou = P(dx, dy);
    P houseki[n];
    REP(i, n) {
      double x, y;
      cin >> x >> y;
      houseki[i] = P(x, y);
    }
    double dp[1 << n][n];
    REP(i, 1 << n) REP(j, n + 1) dp[i][j] = INF;

    REP(i, n) {
      if (abs(houseki[i] - syoki) < abs(houseki[i] - maou))
        dp[1 << i][i] = abs(houseki[i] - syoki);
    }
    REP(S, 1 << n) {
      REP(i, n) {
        // cout << bitset<2>(S) << endl;
        // cout << i << " " << dp[S][i] << endl;
        REP(j, n) {
          if (S >> j & 1)
            continue;
          if (dp[S][i] + abs(houseki[i] - houseki[j]) < abs(maou - houseki[j]))
            dp[S | 1 << j][j] =
                min(dp[S | 1 << j][j], dp[S][i] + abs(houseki[i] - houseki[j]));
        }
      }
    }
    double res = *min_element(dp[(1 << n) - 1], dp[(1 << n) - 1]);
    if (res != INF)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}