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

#include <bits/stdc++.h>
#define N 2001
#define int long long
using namespace std;
vector<int> G[N];
int n, K, S;
struct point {
  int x, y, z;
};

int Area() { return S * S * 6; }
int sArea(point a, point b) {
  int x = min(a.x + S, b.x + S), X = max(a.x, b.x);
  int y = min(a.y + S, b.y + S), Y = max(a.y, b.y);
  int z = min(a.z + S, b.z + S), Z = max(a.z, b.z);
  if (y <= Y || x <= X || z <= Z)
    return 0;
  int area = 0;
  area += 2 * (x - X) * (y - Y);
  area += 2 * (y - Y) * (z - Z);
  area += 2 * (z - Z) * (x - X);
  return area;
}

vector<point> A;
int dfs(int pos, int k, int start, int prev) {
  if (prev != -1 && pos == start)
    return 1LL << 55;
  if (k == K)
    return prev == -1 || prev == start ? 0 : -sArea(A[pos], A[start]);

  int res = 1e9;
  for (int nx : G[pos]) {
    if (nx == prev)
      continue;
    int area = Area() - sArea(A[pos], A[nx]);
    res = min(res, area + dfs(nx, k + 1, start, pos));
  }
  return res;
}

signed main() {
  while (1) {
    cin >> n >> K >> S;
    if (!n && !K && !S)
      break;

    A.resize(n);
    for (int i = 0; i < n; i++)
      cin >> A[i].x >> A[i].y >> A[i].z;

    for (int i = 0; i < N; i++)
      G[i].clear();

    for (int i = 0; i < n; i++)
      for (int j = i + 1; j < n; j++) {
        if (!sArea(A[i], A[j]))
          continue;
        G[i].push_back(j);
        G[j].push_back(i);
      }

    int ans = 1e9;
    for (int i = 0; i < n; i++)
      ans = min(ans, Area() + dfs(i, 1, i, -1));
    if (ans == 1e9)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define N 2001
#define int long long
using namespace std;
vector<int> G[N];
int n, K, S;
struct point {
  int x, y, z;
};

int Area() { return S * S * 6; }
int sArea(point a, point b) {
  int x = min(a.x + S, b.x + S), X = max(a.x, b.x);
  int y = min(a.y + S, b.y + S), Y = max(a.y, b.y);
  int z = min(a.z + S, b.z + S), Z = max(a.z, b.z);
  if (y <= Y || x <= X || z <= Z)
    return 0;
  int area = 0;
  area += 2 * (x - X) * (y - Y);
  area += 2 * (y - Y) * (z - Z);
  area += 2 * (z - Z) * (x - X);
  return area;
}

vector<point> A;
int used[N];
int dfs(int pos, int k, int start, int prev) {
  if (k == K)
    return prev == -1 ? 0 : -sArea(A[pos], A[start]);
  if (used[pos]++)
    return 1e9;

  int res = 1e9;
  for (int nx : G[pos]) {
    if (nx == prev)
      continue;
    int area = Area() - sArea(A[pos], A[nx]);
    res = min(res, area + dfs(nx, k + 1, start, pos));
  }
  return res;
}

signed main() {
  while (1) {
    cin >> n >> K >> S;
    if (!n && !K && !S)
      break;

    A.resize(n);
    for (int i = 0; i < n; i++)
      cin >> A[i].x >> A[i].y >> A[i].z;

    for (int i = 0; i < N; i++)
      G[i].clear();

    for (int i = 0; i < n; i++)
      for (int j = i + 1; j < n; j++) {
        if (!sArea(A[i], A[j]))
          continue;
        G[i].push_back(j);
        G[j].push_back(i);
      }

    for (int i = 0; i < n; i++)
      assert(G[i].size() < 3);
    int ans = 1e9;
    for (int i = 0; i < n; i++) {
      memset(used, 0, sizeof(used));
      ans = min(ans, Area() + dfs(i, 1, i, -1));
    }

    if (ans == 1e9)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}