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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>

using namespace std;

int i = 0, j, k, b, n, m, g, kn[100], km[15], sg[15], kg[32768], fr[32768][15],
    bn[32768], e[16], ans[15];
double ti[32768][15], t;
int main() {
  for (j = 1; j < 65536; j *= 2)
    e[i++] = j;
  for (j = 0; j < 32768; j++)
    for (i = 0, bn[j] = 0; i < 15; i++) {
      ti[j][i] = 999999999;
      if ((j & e[i]) > 0)
        bn[j]++;
    }
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> kn[i] >> km[i] >> g;
    sg[i] = g * 20;
    for (j = 0; j < n; j++)
      ti[e[i]][j] = 0;
  }
  for (i = 0; i < e[n]; i++) {
    kg[i] = 0;
    for (j = 0; j < n; j++)
      if ((i & e[j]) > 0)
        kg[i] += sg[j];
  }
  for (i = 0; i < n; i++) {
    for (j = 1; j < e[n]; j++) {
      if (bn[j] != i + 1)
        continue;
      for (k = 0; k < n; k++) {
        if ((j & e[k]) != 0)
          continue;
        for (b = 0; b < n; b++) {
          if ((j & e[b]) == 0)
            continue;
          t = ti[j][b] + (abs(km[k] - km[b]) * (double)(70 + kg[j]) / 2000);
          if (ti[j | e[k]][k] > t) {
            ti[j | e[k]][k] = t;
            fr[j | e[k]][k] = b;
          }
        }
      }
    }
  }
  t = 999999999;
  for (i = 0; i < n; i++)
    if (ti[e[n] - 1][i] < t) {
      t = ti[e[n] - 1][i];
      g = i;
    }
  for (i = n - 1, j = e[n] - 1; i >= 0; i--) {
    ans[i] = kn[g];
    k = g;
    g = fr[j][g];
    j -= e[k];
  }
  for (i = 0; i < n; i++) {
    cout << ans[i];
    if (i < n - 1)
      cout << ' ';
  }
  cout << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>

using namespace std;

int i = 0, j, k, b, n, m, g, kn[100], km[15], sg[15], kg[32768], fr[32768][15],
    bn[32768], e[15], ans[15];
double ti[32768][15], t;
int main() {
  for (j = 1; j < 32768; j *= 2)
    e[i++] = j;
  for (j = 0; j < 32768; j++)
    for (i = 0, bn[j] = 0; i < 15; i++) {
      ti[j][i] = 99999999;
      if ((j & e[i]) > 0)
        bn[j]++;
    }
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> kn[i] >> km[i] >> g;
    sg[i] = g * 20;
    for (j = 0; j < n; j++)
      ti[e[i]][j] = 0;
  }
  for (i = 0; i < e[n]; i++) {
    kg[i] = 0;
    for (j = 0; j < n; j++)
      if ((i & e[j]) > 0)
        kg[i] += sg[j];
  }
  for (i = 0; i < n; i++) {
    for (j = 1; j < e[n]; j++) {
      if (bn[j] != i + 1)
        continue;
      for (k = 0; k < n; k++) {
        if ((j & e[k]) != 0)
          continue;
        for (b = 0; b < n; b++) {
          if ((j & e[b]) == 0)
            continue;
          t = ti[j][b] + (double)(abs(km[k] - km[b]) * (70 + kg[j]) / 2000);
          if (ti[j | e[k]][k] > t) {
            ti[j | e[k]][k] = t;
            fr[j | e[k]][k] = b;
          }
        }
      }
    }
  }
  t = 999999999;
  for (i = 0; i < n; i++)
    if (ti[e[n] - 1][i] < t) {
      t = ti[e[n] - 1][i];
      g = i;
    }
  for (i = n - 1, j = e[n] - 1; i >= 0; i--) {
    ans[i] = kn[g];
    k = g;
    g = fr[j][g];
    j -= e[k];
  }
  for (i = 0; i < n; i++) {
    cout << ans[i];
    if (i < n - 1)
      cout << ' ';
  }
  cout << endl;
  return 0;
}