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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;

struct Info {
  Info() { id = distance = num = 0; }
  Info(int arg_id, double arg_distance, int arg_num) {
    id = arg_id;
    distance = arg_distance;
    num = arg_num;
  }

  double distance;
  int id, num;
};

int main() {

  int POW[16] = {1,   2,   4,    8,    16,   32,   64,    128,
                 256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
  int N, next_state;
  Info info[15];

  double sum;

  scanf("%d", &N);

  double **dp = new double *[N];
  int **pre = new int *[N];
  for (int i = 0; i < N; i++) {
    dp[i] = new double[POW[N]];
    pre[i] = new int[POW[N]];
    for (int k = 0; k < POW[N]; k++) {
      dp[i][k] = DBL_MAX;
      pre[i][k] = -1;
    }
  }

  for (int i = 0; i < N; i++) {
    scanf("%d %lf %d", &info[i].id, &info[i].distance, &info[i].num);
  }

  if (N == 1) {
    printf("0\\n");
    return 0;
  }

  for (int i = 0; i < N; i++) {
    dp[i][POW[i]] = 0.0;
  }

  for (int state = 0; state <= POW[N] - 2; state++) {
    for (int kura = 0; kura < N; kura++) {
      if (dp[kura][state] == DBL_MAX)
        continue;

      sum = 0;
      for (int a = 0; a < N; a++) {
        if (state & (1 << a)) {
          sum += info[a].num;
        }
      }

      for (int a = 0; a < N; a++) {
        if (state & (1 << a)) {
          // Do nothing
        } else {
          next_state = state + POW[a];
          if (dp[a][next_state] >
              dp[kura][state] + fabs(info[a].distance - info[kura].distance) *
                                    ((7.0 + 2.0 * sum) / 200.0)) {
            dp[a][next_state] =
                dp[kura][state] + fabs(info[a].distance - info[kura].distance) *
                                      ((7.0 + 2.0 * sum) / 200.0);
            pre[a][next_state] = 100000 * kura + state;
          }
        }
      }
    }
  }

  double minimum = DBL_MAX;
  int id;

  for (int i = 0; i < N; i++) {
    if (minimum > dp[i][POW[N] - 1]) {
      minimum = dp[i][POW[N] - 1];
      id = i;
    }
  }

  stack<int> S;

  S.push(info[id].id);
  int pre_kura = pre[id][POW[N] - 1] / 100000,
      pre_state = pre[id][POW[N] - 1] % 100000;

  while (true) {
    S.push(info[pre_kura].id);
    id = pre_kura;
    if (pre[pre_kura][pre_state] == -1)
      break;
    pre_kura = pre[id][pre_state] / 100000;
    pre_state = pre[id][pre_state] % 100000;
  }

  printf("%d", S.top());
  S.pop();

  while (!S.empty()) {
    printf(" %d", S.top());
    S.pop();
  }

  printf("\\n");

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;

struct Info {
  Info() { id = distance = num = 0; }
  Info(int arg_id, double arg_distance, int arg_num) {
    id = arg_id;
    distance = arg_distance;
    num = arg_num;
  }

  double distance;
  int id, num;
};

int main() {

  int POW[16] = {1,   2,   4,    8,    16,   32,   64,    128,
                 256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
  int N, next_state;
  Info info[15];

  double sum;

  scanf("%d", &N);

  double **dp = new double *[N];
  int **pre = new int *[N];
  for (int i = 0; i < N; i++) {
    dp[i] = new double[POW[N]];
    pre[i] = new int[POW[N]];
    for (int k = 0; k < POW[N]; k++) {
      dp[i][k] = DBL_MAX;
      pre[i][k] = -1;
    }
  }

  for (int i = 0; i < N; i++) {
    scanf("%d %lf %d", &info[i].id, &info[i].distance, &info[i].num);
  }

  if (N == 1) {
    printf("0\\n");
    return 0;
  }

  for (int i = 0; i < N; i++) {
    dp[i][POW[i]] = 0.0;
  }

  for (int state = 0; state <= POW[N] - 2; state++) {
    for (int kura = 0; kura < N; kura++) {
      if (dp[kura][state] == DBL_MAX)
        continue;

      sum = 0;
      for (int a = 0; a < N; a++) {
        if (state & (1 << a)) {
          sum += info[a].num;
        }
      }

      for (int a = 0; a < N; a++) {
        if (state & (1 << a)) {
          // Do nothing
        } else {
          next_state = state + POW[a];
          if (dp[a][next_state] >
              dp[kura][state] + fabs(info[a].distance - info[kura].distance) *
                                    ((7.0 + 2.0 * sum) / 200.0)) {
            dp[a][next_state] =
                dp[kura][state] + fabs(info[a].distance - info[kura].distance) *
                                      ((7.0 + 2.0 * sum) / 200.0);
            pre[a][next_state] = 100000 * kura + state;
          }
        }
      }
    }
  }

  double minimum = DBL_MAX;
  int id;

  for (int i = 0; i < N; i++) {
    if (minimum > dp[i][POW[N] - 1]) {
      minimum = dp[i][POW[N] - 1];
      id = i;
    }
  }

  stack<int> S;

  S.push(info[id].id);
  int pre_kura = pre[id][POW[N] - 1] / 100000,
      pre_state = pre[id][POW[N] - 1] % 100000;

  while (true) {
    S.push(info[pre_kura].id);
    id = pre_kura;
    pre_kura = pre[id][pre_state] / 100000;
    pre_state = pre[id][pre_state] % 100000;
    if (pre_kura == -1)
      break;
  }

  printf("%d", S.top());
  S.pop();

  while (!S.empty()) {
    printf(" %d", S.top());
    S.pop();
  }

  printf("\\n");

  return 0;
}