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

#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

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

#define INF 10000000000

int main() {
  int n;
  cin >> n;
  vector<Q> d(n);
  for (int i = 0; i < n; i++) {
    cin >> d[i].first >> (d[i].second).first >> (d[i].second).second;
  }
  vector<vector<vector<long long int>>> dp(
      1 << n, vector<vector<long long int>>(n, vector<long long int>(2, INF)));
  for (int i = 0; i < (1 << n); i++) {
    for (int j = 0; j < n; j++) {
      if (i == 0) {
        dp[i][j][0] = dp[i][j][1] = 0;
        for (int k = 0; k < n; k++) {
          if ((i & (1 << k)) == 0) {
            dp[(i | (1 << k))][k][0] = 0;
            dp[(i | (1 << k))][k][1] = (d[k].second).second;
          }
        }
      } else if (dp[i][j][0] != INF) {
        for (int k = 0; k < n; k++) {
          if ((i & (1 << k)) == 0) {
            if (dp[(i | (1 << k))][k][0] >
                dp[i][j][0] + abs((d[j].second).first - (d[k].second).first) *
                                  (70 + 20 * dp[i][j][1])) {
              dp[(i | (1 << k))][k][0] =
                  dp[i][j][0] + abs((d[j].second).first - (d[k].second).first) *
                                    (70 + 20 * dp[i][j][1]);
              dp[(i | (1 << k))][k][1] = dp[i][j][1] + (d[k].second).second;
            }
          }
        }
      }
    }
  }
  long long int MIN = dp[(1 << n) - 1][0][0];
  int p = 0;
  for (int i = 0; i < n; i++) {
    if (dp[(1 << n) - 1][i][0] < MIN) {
      MIN = dp[(1 << n) - 1][i][0];
      p = i;
    }
  }
  stack<int> stk;
  stk.push(p);
  long long int k = (1 << n) - 1;
  long long int r = dp[k][p][0];
  k -= (1 << p);
  while (k != 0) {
    for (int i = 0; i < n; i++) {
      if (i != p && dp[k][i][0] != INF &&
          dp[k][i][0] + abs((d[i].second).first - (d[p].second).first) *
                            (70 + 20 * dp[k][i][1]) ==
              r) {
        r = dp[k][i][0];
        p = i;
        break;
      }
    }
    k -= (1 << p);
    stk.push(p);
  }
  cout << d[stk.top()].first;
  stk.pop();
  while (!stk.empty()) {
    cout << ' ' << d[stk.top()].first;
    stk.pop();
  }
  cout << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

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

#define INF 100000000

int main() {
  int n;
  cin >> n;
  vector<Q> d(n);
  for (int i = 0; i < n; i++) {
    cin >> d[i].first >> (d[i].second).first >> (d[i].second).second;
  }
  vector<vector<vector<long long int>>> dp(
      1 << n, vector<vector<long long int>>(n, vector<long long int>(2, INF)));
  for (int i = 0; i < (1 << n); i++) {
    for (int j = 0; j < n; j++) {
      if (i == 0) {
        dp[i][j][0] = dp[i][j][1] = 0;
        for (int k = 0; k < n; k++) {
          if ((i & (1 << k)) == 0) {
            dp[(i | (1 << k))][k][0] = 0;
            dp[(i | (1 << k))][k][1] = (d[k].second).second;
          }
        }
      } else if (dp[i][j][0] != INF) {
        for (int k = 0; k < n; k++) {
          if ((i & (1 << k)) == 0) {
            if (dp[(i | (1 << k))][k][0] >
                dp[i][j][0] + abs((d[j].second).first - (d[k].second).first) *
                                  (70 + 20 * dp[i][j][1]) / 2000) {
              dp[(i | (1 << k))][k][0] =
                  dp[i][j][0] + abs((d[j].second).first - (d[k].second).first) *
                                    (70 + 20 * dp[i][j][1]) / 2000;
              dp[(i | (1 << k))][k][1] = dp[i][j][1] + (d[k].second).second;
            }
          }
        }
      }
    }
  }
  long long int MIN = dp[(1 << n) - 1][0][0];
  int p = 0;
  for (int i = 0; i < n; i++) {
    if (dp[(1 << n) - 1][i][0] < MIN) {
      MIN = dp[(1 << n) - 1][i][0];
      p = i;
    }
  }
  stack<int> stk;
  stk.push(p);
  long long int k = (1 << n) - 1;
  long long int r = dp[k][p][0];
  k -= (1 << p);
  while (k != 0) {
    for (int i = 0; i < n; i++) {
      if (i != p && dp[k][i][0] != INF &&
          dp[k][i][0] + abs((d[i].second).first - (d[p].second).first) *
                            (70 + 20 * dp[k][i][1]) / 2000 ==
              r) {
        r = dp[k][i][0];
        p = i;
        break;
      }
    }
    k -= (1 << p);
    stk.push(p);
  }
  cout << d[stk.top()].first;
  stk.pop();
  while (!stk.empty()) {
    cout << ' ' << d[stk.top()].first;
    stk.pop();
  }
  cout << endl;
  return 0;
}