Problem p00146 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

#define dump(n) cout << "# " << #n << "=" << (n) << endl
#define debug(n)                                                               \\
  cout << __FILE__ << "," << __LINE__ << ": #" << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, n) repi(i, 0, n)
#define iter(c) __typeof((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define allof(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;

typedef vector<double> vd;
typedef vector<vd> vvd;

int main() {
  int n;
  cin >> n;
  vi id(n), dist(n), box(n);
  rep(i, n) cin >> id[i] >> dist[i] >> box[i];

  // vvd dp(1<<n,vd(n,1e10));
  // vvi prev(1<<n,vi(n));
  static double dp[1 << 15][15];
  static int prev[1 << 15][15] = {};
  rep(i, 1 << 15) fill(dp[i], dp[i] + 15, 1e10);
  rep(i, n) {
    dp[1 << i][i] = 0;
    prev[1 << i][i] = -1;
  }
  rep(i, 1 << n) {
    int weight = 0;
    rep(j, n) if (1 << j & i) weight += box[j] * 20;
    rep(j, n) rep(k, n) {
      if (!((1 << j) & i) || ((1 << k) & i))
        continue;
      double time =
          dp[i][j] + abs(dist[k] - dist[j]) / (2000.0 / (70 + weight));
      if (dp[i | (1 << k)][k] > time) {
        dp[i | (1 << k)][k] = time;
        prev[i | (1 << k)][k] = j;
      }
    }
  }

  vi res;
  {
    int i = 0;
    rep(j, n) if (dp[(1 << n) - 1][j] < dp[(1 << n) - 1][i]) i = j;
    for (int j = (1 << n) - 1; i != -1;) {
      res.push_back(i);
      int ii = prev[j][i];
      int jj = j ^ (1 << i);
      i = ii;
      j = jj;
    }
  }

  reverse(allof(res));
  rep(i, res.size())
      printf("%d%c", id[res[i]], i == res.size() - 1 ? '\\n' : ' ');

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

#define dump(n) cout << "# " << #n << "=" << (n) << endl
#define debug(n)                                                               \\
  cout << __FILE__ << "," << __LINE__ << ": #" << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, n) repi(i, 0, n)
#define iter(c) __typeof((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define allof(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;

typedef vector<double> vd;
typedef vector<vd> vvd;

int main() {
  int n;
  cin >> n;
  vi id(n), dist(n), box(n);
  rep(i, n) cin >> id[i] >> dist[i] >> box[i];

  vvd dp(1 << n, vd(n, 9999));
  vvi prev(1 << n, vi(n));
  rep(i, n) {
    dp[1 << i][i] = 0;
    prev[1 << i][i] = -1;
  }
  rep(i, 1 << n) {
    int weight = 0;
    rep(j, n) if (1 << j & i) weight += box[j] * 20;
    rep(j, n) rep(k, n) {
      if (!((1 << j) & i) || ((1 << k) & i))
        continue;
      double time =
          dp[i][j] + abs(dist[k] - dist[j]) / (2000.0 / (70 + weight));
      if (dp[i | (1 << k)][k] > time) {
        dp[i | (1 << k)][k] = time;
        prev[i | (1 << k)][k] = j;
      }
    }
  }

  vi res;
  {
    int i = 0;
    rep(j, n) if (dp[(1 << n) - 1][j] < dp[(1 << n) - 1][i]) i = j;
    for (int j = (1 << n) - 1; i != -1;) {
      res.push_back(i);
      int ii = prev[j][i];
      int jj = j ^ (1 << i);
      i = ii;
      j = jj;
    }
  }

  reverse(allof(res));
  rep(i, res.size())
      printf("%d%c", id[res[i]], i == res.size() - 1 ? '\\n' : ' ');

  return 0;
}