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

#include <bits/stdc++.h>
#define loop for (;;)
typedef std::vector<long> LI;
template <typename T> void scan1(T &x) { fprintf(stderr, "unknown type\\n"); }
template <> void scan1(long &x) {
  if (scanf("%ld", &x) < 0)
    exit(0);
}
template <> void scan1(std::string &x) {
  if (not(std::cin >> x))
    exit(0);
}
void scan() {}
template <typename Head, typename... Tail> void scan(Head &x, Tail &...xs) {
  scan1(x);
  scan(xs...);
}

struct Solver {
  std::vector<long> nexts[2123];
  std::vector<long> overlap[2123];
  long n, len, s;
  LI xs, ys, zs;
  long f(long i, long k) {
    long dx = std::max((long)0, s - std::abs(xs[k] - xs[i]));
    long dy = std::max((long)0, s - std::abs(ys[k] - ys[i]));
    long dz = std::max((long)0, s - std::abs(zs[k] - zs[i]));
    if (dx == 0 or dy == 0 or dz == 0)
      return 0;
    return 2 * (dx * dy + dy * dz + dz * dx);
  }
  void solve() {
    scan(n, len, s);
    if (n == 0 and len == 0 and s == 0)
      exit(0);
    xs.resize(n), ys.resize(n), zs.resize(n);
    for (long i = 0; i < n; ++i)
      scan(xs[i], ys[i], zs[i]);
    for (long i = 0; i < n; ++i) {
      for (long k = 0; k < n; ++k) {
        if (i == k)
          continue;
        long over = f(i, k);
        if (over == 0)
          continue;
        nexts[i].push_back(k);
        overlap[i].push_back(over);
      }
    }
    long res = -(1LL << 53);
    for (long i = 0; i < n; ++i) {
      visited.resize(n, false);
      res = std::max(res, dfs(i, len - 1, i));
    }
    visited.resize(n);
    printf("%ld\\n", res >= 0 ? 6 * s * s * len - res : -1);
  }
  std::vector<bool> visited;
  long dfs(long v, long d, long begin) {
    if (d == 0) {
      if (len >= 3) {
        for (long i = 0; i < (long)nexts[v].size(); ++i) {
          long next = nexts[v][i];
          long over = overlap[v][i];
          if (next == begin) {
            return over;
          }
        }
      }
      return 0;
    }
    long res = -(1LL << 53);
    visited[v] = true;
    for (long i = 0; i < (long)nexts[v].size(); ++i) {
      long next = nexts[v][i];
      long over = overlap[v][i];
      if (visited[next])
        continue;
      res = std::max(res, dfs(next, d - 1, begin) + over);
    }
    visited[v] = false;
    return res;
  }
};

int main() { loop std::shared_ptr<Solver>(new Solver())->solve(); }

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define loop for (;;)
typedef std::vector<long> LI;
template <typename T> void scan1(T &x) { fprintf(stderr, "unknown type\\n"); }
template <> void scan1(long &x) {
  if (scanf("%ld", &x) < 0)
    exit(0);
}
template <> void scan1(std::string &x) {
  if (not(std::cin >> x))
    exit(0);
}
void scan() {}
template <typename Head, typename... Tail> void scan(Head &x, Tail &...xs) {
  scan1(x);
  scan(xs...);
}

struct Solver {
  std::vector<long> nexts[2123];
  std::vector<long> overlap[2123];
  long n, len, s;
  LI xs, ys, zs;
  long f(long i, long k) {
    long dx = std::max((long)0, s - std::abs(xs[k] - xs[i]));
    long dy = std::max((long)0, s - std::abs(ys[k] - ys[i]));
    long dz = std::max((long)0, s - std::abs(zs[k] - zs[i]));
    if (dx == 0 or dy == 0 or dz == 0)
      return 0;
    return 2 * (dx * dy + dy * dz + dz * dx);
  }
  void solve() {
    scan(n, len, s);
    if (n == 0 and len == 0 and s == 0)
      exit(0);
    xs.resize(n), ys.resize(n), zs.resize(n);
    for (long i = 0; i < n; ++i)
      scan(xs[i], ys[i], zs[i]);
    for (long i = 0; i < n; ++i) {
      for (long k = 0; k < n; ++k) {
        if (i == k)
          continue;
        long over = f(i, k);
        if (over == 0)
          continue;
        nexts[i].push_back(k);
        overlap[i].push_back(over);
        fprintf(stderr, "%ld %ld %ld\\n", i, k, over);
      }
    }
    long res = 0;
    for (long i = 0; i < n; ++i) {
      visited.resize(n, false);
      res = std::max(res, dfs(i, len - 1, i));
    }
    visited.resize(n);
    printf("%ld\\n", res > 0 ? 6 * s * s * len - res : -1);
  }
  std::vector<bool> visited;
  long dfs(long v, long d, long begin) {
    if (d == 0) {
      for (long i = 0; i < (long)nexts[v].size(); ++i) {
        long next = nexts[v][i];
        long over = overlap[v][i];
        if (next == begin) {
          return over;
        }
      }
      return 0;
    }
    long res = -(1LL << 53);
    visited[v] = true;
    for (long i = 0; i < (long)nexts[v].size(); ++i) {
      long next = nexts[v][i];
      long over = overlap[v][i];
      if (visited[next])
        continue;
      res = std::max(res, dfs(next, d - 1, begin) + over);
    }
    visited[v] = false;
    return res;
  }
};

int main() { loop std::shared_ptr<Solver>(new Solver())->solve(); }