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

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define INF 1e16

ll n, k, s;
ll x[2222], y[2222], z[2222];
bool used[2002];
vector<int> g[2222];

bool is_cross(int i, int j) {
  return (abs(x[i] - x[j]) <= s) && (abs(y[i] - y[j]) <= s) &&
         (abs(z[i] - z[j]) <= s);
}

ll cross_surf(int i, int j) {
  ll xx = s - abs(x[i] - x[j]);
  ll yy = s - abs(y[i] - y[j]);
  ll zz = s - abs(z[i] - z[j]);
  return 2 * (xx * yy + yy * zz + zz * xx);
}

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    rep(i, 2222) g[i].clear();
    cin >> n >> k >> s;
    if (n == 0)
      break;
    rep(i, n) { cin >> x[i] >> y[i] >> z[i]; }
    if (k == 1) {
      cout << s * s * 6 << endl;
      continue;
    }
    rep(i, n) rep(j, i) {
      if (is_cross(i, j)) {
        g[i].pb(j);
        g[j].pb(i);
      }
    }
    ll res = INF;
    rep(i, n) {
      for (int j : g[i]) {
        memset(used, 0, sizeof(used));
        used[i] = used[j] = true;
        ll sum = s * s * 6 * 2 - cross_surf(i, j);
        int cnt = 2;
        int last;
        rep(hoge, k - 2) {
          for (int nxt : g[j]) {
            if (used[nxt])
              continue;
            used[nxt] = true;
            sum += s * s * 6 - cross_surf(j, nxt);
            last = nxt;
            cnt++;
            j = nxt;
          }
        }
        if (cnt == k)
          minch(res,
                sum - (k > 2 && is_cross(i, last) ? cross_surf(i, last) : 0));
      }
    }
    if (res == INF)
      cout << -1 << endl;
    else
      cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF 1e16

#define MAX_N 1000

ll n, k, s;
ll x[2222], y[2222], z[2222];
vector<int> g[2222];
bool used[2222];

bool is_cross(int i, int j) {
  return (abs(x[i] - x[j]) <= s) && (abs(y[i] - y[j]) <= s) &&
         (abs(z[i] - z[j]) <= s);
}

ll cross_surf(int i, int j) {
  ll xx = s - abs(x[i] - x[j]);
  ll yy = s - abs(y[i] - y[j]);
  ll zz = s - abs(z[i] - z[j]);
  return 2 * (xx * yy + yy * zz + zz * xx);
}

ll dfs(int i, int d, int pre, ll sum) {
  if (d == k - 1) {
    if (g[i].size() == 2 && used[g[i][0]] && used[g[i][1]])
      return sum - cross_surf(i, pre);
    else
      return sum;
  } else {
    ll res = INF;
    used[i] = true;
    for (int j : g[i]) {
      if (used[j])
        continue;
      minch(res, dfs(j, d + 1, i, sum + s * s * 6 - cross_surf(i, j)));
    }
    used[i] = false;
    return res;
  }
}

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    memset(used, 0, sizeof(used));
    rep(i, 2222) g[i].clear();
    cin >> n >> k >> s;
    if (n == 0)
      break;
    rep(i, n) { cin >> x[i] >> y[i] >> z[i]; }
    rep(i, n) rep(j, i) {
      if (is_cross(i, j)) {
        g[i].pb(j);
        g[j].pb(i);
      }
    }
    ll res = INF;
    rep(i, n) { minch(res, dfs(i, 0, -1, s * s * 6)); }
    if (res == INF)
      cout << -1 << endl;
    else
      cout << res << endl;
  }
  return 0;
}