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

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define PI 3.1415926535897932384626433832795028

using namespace std;
struct point {
  double x, y, z;
} a[30];
int n, m, num[1000];
double map[100][100];
double f() {
  double k = 0;
  for (int i = 0; i < n; i++) {
    if (num[i] == 0) {
      for (int j = i + 1; j < n; j++) {
        if (num[j] == 0 && i != j) {
          k += map[i][j];
        }
      }
    }
  }
  return k;
}
int main() {
  int i, j, nx, ny, k;
  double max1, max2, sum, ans, temp;
  while (scanf("%d%d", &n, &m) != EOF) {
    for (i = 0; i < n; i++) {
      scanf("%lf%lf%lf", &a[i].x, &a[i].y, &a[i].z);
    }
    if (m == 1) {
      printf("0.00000000000000000000\\n");
      continue;
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        if (i != j) {
          map[i][j] = (a[i].x - a[j].x) * (a[i].x - a[j].x) +
                      (a[i].y - a[j].y) * (a[i].y - a[j].y) +
                      (a[i].z - a[j].z) * (a[i].z - a[j].z);
        }
      }
    }
    ans = 0;
    for (int j = (1 << m) - 1; j < 1 << n; j++) {
      memset(num, 1, sizeof(num));
      int count = 0;
      for (int i = 0; i < n; i++) {
        if (j & (1 << i))
          num[i] = 0, count++;
      }
      if (count == m) {
        temp = f();
        if (temp > ans)
          ans = temp;
      }
    }
    printf("%.20lf\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef long long LL;
const LL maxn = 222;
double a[maxn];
double l[maxn], b[maxn];
double mp[maxn][maxn];
LL vis[maxn];
LL que[maxn * maxn * 50];

double Sum(LL i, LL j) {
  return (l[i] - l[j]) * (l[i] - l[j]) + (a[i] - a[j]) * (a[i] - a[j]) +
         (b[i] - b[j]) * (b[i] - b[j]);
}

int main() {
  LL n, i, m, j;
  double ans, Max, total;
  LL x, y, tail;
  scanf("%lld%lld", &n, &m);
  for (i = 1; i <= n; ++i) {
    scanf("%lf%lf%lf", &l[i], &a[i], &b[i]);
  }
  if (m == 1 || m == 0)
    printf("0.00000000000000000000\\n");
  else {
    memset(vis, 0, sizeof(vis));
    memset(mp, 0, sizeof(mp));
    Max = 0;
    for (i = 1; i <= n; ++i) {
      for (j = i + 1; j <= n; ++j) {
        mp[j][i] = mp[i][j] = Sum(i, j);
        if (Max < mp[i][j]) {
          Max = mp[i][j];
          x = i, y = j;
        }
      }
    }
    vis[x] = vis[y] = 1;
    m -= 2;
    ans = Max;
    que[0] = x, que[1] = y;
    tail = 2;
    while (m--) {
      Max = 0;
      for (i = 1; i <= n; ++i) {
        total = 0;
        if (vis[i])
          continue;
        for (j = 0; j < tail; ++j) {
          total += mp[i][que[j]];
        }
        if (Max < total) {
          Max = total;
          x = i;
        }
      }
      vis[x] = 1, que[tail++] = x;
      ans += Max;
    }
    printf("%.20lf\\n", ans);
  }
  return 0;
}