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

// 29
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

/*
double t[1<<15][15];
int p[1<<15][15];
int n;
int k[15],d[15],s[15];

void dfs(int f,int ds){
  if(t[f][ds]!=1<<30)return;
  for(int i=0;i<n;i++){
    if(f&1<<i){
      int nf=f&~(1<<i);
      dfs(nf,i);
      double nt=t[nf][i];
      int w=0;
      for(int j=0;j<n;j++){
        if(nf&1<<j){
          w+=s[j]*20;
        }
      }
      nt+=abs(d[ds]-d[i])/(2000./(70+w));
      if(nt<t[f][ds]){
        t[f][ds]=nt;
        p[f][ds]=i;
      }
    }
  }
}



int main(){
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>k[i]>>d[i]>>s[i];
  }
  fill(t[0],t[1<<15],1<<30);
  fill(p[0],p[1<<15],-1);
  for(int i=0;i<n;i++){
    t[0][i]=0;
  }
  int mn=1<<30;
  int x;
  for(int i=0;i<n;i++){
    dfs((1<<n)-1,i);
    if(mn>t[(1<<n)-1][i]){
      mn=t[(1<<n)-1][i];
      x=i;
    }
  }
  vector<int> v;
  int f=(1<<n)-1;
  while(x!=-1){
    v.push_back(x);
    int nf=f&~(1<<x);
    x=p[f][x];
    f=nf;
  }
  for(int i=0;i<n;i++){
    cout<<k[v[n-1-i]]<<((i!=n-1)?' ':'\\n');
  }
  return 0;
}
*/

struct S {
  int w, f, i, p;
  double t;
  bool operator<(S a) const { return t > a.t; }
};

int main() {
  int n;
  cin >> n;
  int k[15], d[15], s[15];
  for (int i = 0; i < n; i++) {
    cin >> k[i] >> d[i] >> s[i];
  }
  priority_queue<S> que;
  for (int i = 0; i < n; i++) {
    S is;
    is.t = 0;
    is.f = 1 << i;
    is.i = i;
    is.w = s[i];
    is.p = -1;
    que.push(is);
  }
  bool p[1 << 15][15] = {{}};
  static int pr[1 << 15][15];
  for (;;) {
    S c = que.top();
    if (c.f == (1 << n) - 1)
      break;
    que.pop();
    if (p[c.f][c.i]++)
      continue;
    pr[c.f][c.i] = c.p;
    for (int i = 0; i < n; i++) {
      S n = c;
      n.t += abs(d[c.i] - d[i]) / (2000. / (70 + c.w * 20));
      n.f |= 1 << i;
      n.i = i;
      n.w += s[i];
      n.p = c.i;
      if (!p[n.f][n.i]) {
        que.push(n);
      }
    }
  }
  int x = que.top().i;
  int f = (1 << n) - 1;
  int a[15];
  for (int i = 0; i < n; i++) {
    int nf = f & ~(1 << x);
    a[i] = k[x];
    x = (i == 0) ? que.top().p : pr[f][x];
    f = nf;
  }
  for (int i = 0; i < n; i++) {
    cout << a[n - 1 - i] << ((i == n - 1) ? '\\n' : ' ');
  }
  return 0;
}

Error: MLE

Fixed submission:

// 29
#include <cstdlib>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct S {
  int w, f, i;
  double t;
  vector<char> v;
  bool operator<(S a) const { return t > a.t; }
};

int main() {
  int n;
  cin >> n;
  int k[15], d[15], s[15];
  for (int i = 0; i < n; i++) {
    cin >> k[i] >> d[i] >> s[i];
  }
  priority_queue<S> que;
  for (int i = 0; i < n; i++) {
    S is;
    is.t = 0;
    is.f = 1 << i;
    is.i = i;
    is.w = s[i];
    is.v.push_back(k[i]);
    que.push(is);
  }
  bool p[1 << 15][15] = {{}};
  for (;;) {
    S c = que.top();
    if (c.f == (1 << n) - 1)
      break;
    que.pop();
    if (p[c.f][c.i]++)
      continue;
    for (int i = 0; i < n; i++) {
      S n = c;
      n.t += abs(d[c.i] - d[i]) / (2000. / (70 + c.w * 20));
      n.f |= 1 << i;
      n.i = i;
      n.w += s[i];
      n.v.push_back(k[i]);
      if (!p[n.f][n.i]) {
        que.push(n);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << (int)que.top().v[i] << ((i == n - 1) ? '\\n' : ' ');
  }
  return 0;
}