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

#include <bits/stdc++.h>
#define INF 1000000000000007LL
using namespace std;

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

int n, k, s;
vector<P> G[2001];
bool flag[2001];
int x[2001], y[2001], z[2001];
int cnt = 0;
ll sum = 0;

bool check(int i, int j) {
  if (x[i] + s <= x[j] || x[j] + s <= x[i])
    return false;
  if (y[i] + s <= y[j] || y[j] + s <= y[i])
    return false;
  if (z[i] + s <= z[j] || z[j] + s <= z[i])
    return false;
  return true;
}

void dfs(int v, int p) {
  flag[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    if (G[v][i].first != p) {
      if (flag[G[v][i].first])
        sum -= (ll)G[v][i].second;
      else {
        if (cnt < k) {
          sum += (ll)6 * s * s - G[v][i].second;
          cnt++;
          dfs(G[v][i].first, v);
        }
      }
    }
  }
}

int main(void) {
  while (1) {
    scanf("%d%d%d", &n, &k, &s);
    if (n == 0 && k == 0 && s == 0)
      return 0;
    for (int i = 0; i < n; i++) {
      G[i].clear();
      scanf("%d%d%d", &x[i], &y[i], &z[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (check(i, j)) {
          int v1 = s - abs(x[i] - x[j]);
          int v2 = s - abs(y[i] - y[j]);
          int v3 = s - abs(z[i] - z[j]);
          ll cost = 2 * (v1 * v2 + v2 * v3 + v3 * v1);
          G[i].push_back(P(j, cost));
          G[j].push_back(P(i, cost));
        }
      }
    }
    ll res = INF;
    for (int i = 0; i < n; i++) {
      if (k == 1) {
        res = min(res, (ll)6 * s * s);
        continue;
      }
      for (int j = 0; j < G[i].size(); j++) {
        memset(flag, false, sizeof(flag));
        flag[i] = true;
        flag[G[i][j].first] = true;
        cnt = 2;
        sum = 12 * s * s - G[i][j].second;
        dfs(G[i][j].first, i);
        if (cnt == k)
          res = min(res, sum);
      }
    }
    printf("%lld\\n", res == INF ? -1 : res);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define INF 1000000000000007LL
using namespace std;

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

int n, k, s;
vector<P> G[101];
bool flag[101];
int x[101], y[101], z[101];
int cnt = 0;
ll sum = 0;

bool check(int i, int j) {
  if (x[i] + s <= x[j] || x[j] + s <= x[i])
    return false;
  if (y[i] + s <= y[j] || y[j] + s <= y[i])
    return false;
  if (z[i] + s <= z[j] || z[j] + s <= z[i])
    return false;
  return true;
}

void dfs(int v, int p) {
  flag[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    if (G[v][i].first != p) {
      if (flag[G[v][i].first])
        sum -= (ll)G[v][i].second;
      else {
        if (cnt < k) {
          sum += (ll)6 * s * s - G[v][i].second;
          cnt++;
          dfs(G[v][i].first, v);
        }
      }
    }
  }
}

int main(void) {
  while (1) {
    scanf("%d%d%d", &n, &k, &s);
    if (n == 0 && k == 0 && s == 0)
      return 0;
    for (int i = 0; i < n; i++) {
      G[i].clear();
      scanf("%d%d%d", &x[i], &y[i], &z[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (check(i, j)) {
          int v1 = s - abs(x[i] - x[j]);
          int v2 = s - abs(y[i] - y[j]);
          int v3 = s - abs(z[i] - z[j]);
          ll cost = 2 * (v1 * v2 + v2 * v3 + v3 * v1);
          G[i].push_back(P(j, cost));
          G[j].push_back(P(i, cost));
        }
      }
    }
    ll res = INF;
    for (int i = 0; i < n; i++) {
      if (k == 1) {
        res = min(res, (ll)6 * s * s);
        continue;
      }
      for (int j = 0; j < G[i].size(); j++) {
        memset(flag, false, sizeof(flag));
        flag[i] = true;
        flag[G[i][j].first] = true;
        cnt = 2;
        sum = 12 * s * s - G[i][j].second;
        dfs(G[i][j].first, i);
        if (cnt == k)
          res = min(res, sum);
      }
    }
    printf("%lld\\n", res == INF ? -1 : res);
  }
  return 0;
}