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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
struct pos {
  int x;
  int y;
};
struct rel {
  int to;
  int dis;
};
int main() {
  int n, hx, hy, dx, dy;
  while (cin >> n >> hx >> hy >> dx >> dy, n | hx | hy | dx | dy) {
    vector<pos> p;
    REP(k, n) {
      int x, y;
      cin >> x >> y;
      p.push_back({x, y});
    }
    vector<vector<double>> r(n + 1, vector<double>(n + 1));
    vector<double> dr(n);
    REP(i, n + 1) {
      if (i != n)
        dr[i] = hypot(dx - p[i].x, dy - p[i].y);

      for (int j = 0; j < n + 1; j++) {
        if (i == j)
          continue;
        if (i != n && j != n)
          r[i][j] = hypot(p[i].x - p[j].x, p[i].y - p[j].y);
        else if (i == n)
          r[i][j] = hypot(hx - p[j].x, hy - p[j].y);
        else if (j == n)
          r[i][j] = hypot(p[i].x - hx, p[i].y - hy);
      }
    }

    bool b[21] = {false};
    REP(i, n) b[i] = true;
    int b_n = n;
    bool flag = false;
    function<void(double dis, int from)> f = [&](double dis, int from) {
      if (flag)
        return;
      for (int i = 0; i < n; i++) {
        if (b[i] && (dis + r[from][i] + 1e-8) >= dr[i])
          return;
      }

      if (b_n != 0) {
        for (int i = 0; i < n && !flag; i++) {
          if (b[i]) {
            b[i] = false;
            b_n--;
            f(dis + r[from][i], i);
            b[i] = true;
            b_n++;
          }
        }
      } else {
        flag = true;
        return;
      }

      return;
    };
    f(0.0, n);
    cout << (flag ? "YES" : "NO") << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
struct pos {
  int x;
  int y;
};
struct rel {
  int to;
  int dis;
};
int dist(int x, int y) { return x * x + y * y; }
int main() {
  int n, hx, hy, dx, dy;
  while (cin >> n >> hx >> hy >> dx >> dy) {
    vector<pos> p;
    REP(k, n) {
      int x, y;
      cin >> x >> y;
      p.push_back({x, y});
    }
    vector<vector<pos>> r(n + 1, vector<pos>(n + 1));
    vector<pos> dr(n);
    REP(i, n + 1) {
      if (i != n)
        dr[i].x = dx - p[i].x;
      dr[i].y = dy - p[i].y;

      for (int j = 0; j < n + 1; j++) {
        if (i == j)
          continue;
        if (i != n && j != n) {
          r[i][j].x = (p[i].x - p[j].x);
          r[i][j].y = (p[i].y - p[j].y);
        } else if (i == n) {
          r[i][j].x = (hx - p[j].x);
          r[i][j].y = (hy - p[j].y);
        } else if (j == n) {
          r[i][j].x = (p[i].x - hx);
          r[i][j].y = (p[i].y - hy);
        }
      }
    }

    bool b[21] = {false};
    REP(i, n) b[i] = true;
    int b_n = n;
    bool flag = false;
    function<bool(int dis, int from)> f = [&](int dis, int from) {
      if (flag)
        return true;
      for (int i = 0; i < n; i++) {
        if (b[i] && (dis + dist(dr[i].x, dr[i].y)) >= dist(dr[i].x, dr[i].y))
          return false;
      }

      if (b_n != 0) {
        for (int i = 0; i < n && !flag; i++) {
          if (b[i] && (dis + dist(dr[i].x, dr[i].y)) < dist(dr[i].x, dr[i].y)) {
            b[i] = false;
            b_n--;
            f(dis + dist(dr[i].x, dr[i].y), i);
            b[i] = true;
            b_n++;
          }
        }
      } else {
        flag = true;
        return true;
      }

      return flag;
    };
    f(0, n);
    cout << (flag ? "YES" : "NO") << endl;
  }
}