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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int N;
vector<int> id;
vector<int> weight;
vector<int> dist;

double cost(int from, int to, int bit) {
  double w = 0;
  for (int i = 0; i < N; i++) {
    if (bit & (1 << i))
      w += weight[i];
  }
  return abs(dist[from] - dist[to]) / (2000 / (70 + w));
}

typedef pair<int, int> P;
typedef pair<double, vector<int>> PP;

const double INF = 1e9;

const int MAX_N = 15;
PP dp[MAX_N][1 << MAX_N];

/*
pair<double, vector<int> > tsp(int v, int b) {
    if(dp[v][b].first != -1) return dp[v][b];
    if(b == (1 << N) - 1) {
        PP res;
        res.first = 0;
        res.second.push_back(id[v]);
        return dp[v][b] = res;
    }

    PP res;
    res.first = 1e9;

    for(int i = 0; i < N; i++) {
        if(b & (1 << i)) continue;
        PP r = tsp(i, b | (1 << i));
        r.first += cost(v, i, b);
        if(r.first < res.first) {
            res = r;
        }
    }

    if(b != 0) res.second.insert(res.second.begin(), id[v]);
    return dp[v][b] = res;
}
*/

int main() {
  cin >> N;
  id.clear();
  weight.clear();
  dist.clear();
  for (int i = 0; i < N; i++) {
    int num, d, box;
    cin >> num >> d >> box;
    id.push_back(num);
    dist.push_back(d);
    weight.push_back(box * 20);
  }

  for (int v = 0; v < N; v++) {
    for (int s = 0; s < (1 << N); s++) {
      dp[v][s].first = INF;
      dp[v][s].second.clear();
    }
  }

  for (int v = 0; v < N; v++) {
    dp[v][(1 << N) - 1].first = 0;
  }

  for (int S = (1 << N) - 2; S >= 0; S--) {
    for (int v = 0; v < N; v++) {
      for (int u = 0; u < N; u++) {
        PP r = dp[u][S | (1 << u)];
        r.first += cost(v, u, S);
        r.second.push_back(id[u]);
        if (r.first < dp[v][S].first) {
          dp[v][S] = r;
        }
      }
    }
  }

  PP res;
  res.first = INF;
  for (int i = 0; i < N; i++) {
    // PP r = tsp(i, 0);
    PP r = dp[i][0];
    if (r.first < res.first) {
      res = r;
    }
  }

  cout << *res.second.rbegin();
  for (int i = res.second.size() - 2; i >= 0; i--) {
    cout << ' ' << res.second[i];
  }
  cout << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int N;
vector<int> id;
vector<int> weight;
vector<int> dist;

double cost(int from, int to, int bit) {
  int w = 0;
  for (int i = 0; i < N; i++) {
    if (bit & (1 << i))
      w += weight[i];
  }
  return abs(dist[from] - dist[to]) / (2000 / (70 + w));
}

typedef pair<int, int> P;
map<P, pair<double, vector<int>>> memo;

pair<double, vector<int>> tsp(int v, int b) {
  if (memo.count(P(v, b)))
    return memo[P(v, b)];
  if (b == (1 << N) - 1) {
    vector<int> ord;
    ord.push_back(id[v]);
    return memo[P(v, b)] = pair<double, vector<int>>(0, ord);
  }

  vector<int> ord;
  double min_dist = 1e9;
  for (int i = 0; i < N; i++) {
    if (b & (1 << i))
      continue;
    int S = b | (1 << i);
    pair<double, vector<int>> res = tsp(i, S);
    int D = res.first + cost(v, i, S);
    if (D < min_dist) {
      min_dist = D;
      ord = res.second;
    }
  }

  vector<int> res;
  if (b != 0)
    res.push_back(id[v]);
  for (int i = 0; i < (int)ord.size(); i++)
    res.push_back(ord[i]);
  return memo[P(v, b)] = pair<double, vector<int>>(min_dist, res);
}

int main() {
  cin >> N;
  id.clear();
  weight.clear();
  for (int i = 0; i < N; i++) {
    int num, d, box;
    cin >> num >> d >> box;
    id.push_back(num);
    dist.push_back(d);
    weight.push_back(box * 20);
  }

  vector<int> order;
  double min_dist = 1e9;
  for (int i = 0; i < N; i++) {
    pair<double, vector<int>> res = tsp(i, 0);
    if (res.first < min_dist) {
      min_dist = res.first;
      order = res.second;
    }
  }
  cout << *order.begin();
  for (int i = 1; i < (int)order.size(); i++) {
    cout << ' ' << order[i];
  }
  cout << endl;
}