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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define INF 1e+100
#define EPS 1e-9
#define PB push_back
#define fi first
#define se second
#define reps(i, j, k) for (int i = j; i < k; i++)
#define rep(i, j) reps(i, 0, j)
using namespace std;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
typedef long long ll;
typedef pair<int, int> Pii;
class Data {
public:
  int id;
  int dist;
  int money;
  Data() {}
  Data(int a, int b, int c) {
    id = a;
    dist = b;
    money = c;
  }
};
vector<Data> D;
double dp[16][1 << 16];
vector<int> path[16][1 << 16];
vector<int> ans;
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    D.PB(Data(x, y, z));
  }
  int weight[1 << 16];
  double cost[16][16];
  rep(i, n) {
    rep(j, 1 << n) { dp[i][j] = INF; }
  }
  rep(i, n) {
    rep(j, n) { cost[i][j] = (double)abs(D[i].dist - D[j].dist); }
  }
  rep(i, n) {
    dp[i][1 << i] = 0;
    path[i][1 << i].PB(i);
  }
  rep(i, 1 << n) {
    int res = 0;
    rep(j, n) {
      if (i >> j & 1)
        res += D[j].money;
    }
    weight[i] = 20 * res;
  }
  rep(i, 1 << n) {
    double res = (double)2000.0 / (70 + weight[i]);
    rep(j, n) {
      int now = 1 << j;
      if ((now & i) != 0) {
        rep(k, n) {
          int next = 1 << k;
          if ((next & i) <= 0) {
            double sub = cost[j][k] / res;
            if (dp[k][i | next] > dp[j][i] + sub) {
              dp[k][i | next] = dp[j][i] + sub;
              path[k][i | next] = path[j][i];
              path[k][i | next].PB(k);
            }
          }
        }
      }
    }
  }
  double mi = INF;
  int len;
  rep(i, n) {
    if (mi > dp[i][(1 << n) - 1]) {
      mi = dp[i][(1 << n) - 1];
      ans = path[i][(1 << n) - 1];
    }
  }
  len = ans.size();
  rep(i, len) { printf("%d%c", D[ans[i]].id, i == len - 1 ? '\\n' : ' '); }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define INF 1e+8
#define EPS 1e-9
#define PB push_back
#define fi first
#define se second
#define reps(i, j, k) for (int i = j; i < k; i++)
#define rep(i, j) reps(i, 0, j)
using namespace std;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
typedef long long ll;
typedef pair<int, int> Pii;
class Data {
public:
  int id;
  int dist;
  int money;
  Data() {}
  Data(int a, int b, int c) {
    id = a;
    dist = b;
    money = c;
  }
};
vector<Data> D;
double dp[16][1 << 16];
vector<int> path[16][1 << 16];
vector<int> ans;
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    D.PB(Data(x, y, z));
  }
  int weight[16];
  double cost[16][16];
  rep(i, n) {
    rep(j, 1 << n) { dp[i][j] = INF; }
  }
  rep(i, n) {
    rep(j, n) { cost[i][j] = (double)abs(D[i].dist - D[j].dist); }
  }
  rep(i, n) {
    dp[i][1 << i] = 0;
    path[i][1 << i].PB(i);
  }
  rep(i, 1 << n) {
    int res = 0;
    rep(j, n) {
      if (i >> j & 1)
        res += D[j].money;
    }
    weight[i] = 20 * res;
  }
  rep(i, 1 << n) {
    double res = (double)2000.0 / (70 + weight[i]);
    rep(j, n) {
      int now = 1 << j;
      if ((now & i) != 0) {
        rep(k, n) {
          int next = 1 << k;
          if ((next & i) <= 0) {
            double sub = cost[j][k] / res;
            if (dp[k][i | next] > dp[j][i] + sub) {
              dp[k][i | next] = dp[j][i] + sub;
              path[k][i | next] = path[j][i];
              path[k][i | next].PB(k);
            }
          }
        }
      }
    }
  }
  double mi = INF;
  int len;
  rep(i, n) {
    if (mi > dp[i][(1 << n) - 1]) {
      mi = dp[i][(1 << n) - 1];
      ans = path[i][(1 << n) - 1];
    }
  }
  len = ans.size();
  rep(i, len) { printf("%d%c", D[ans[i]].id, i == len - 1 ? '\\n' : ' '); }
  return 0;
}