Problem p00146 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

typedef long long ll;
const double Eps = 1e-6;

using namespace std;

typedef pair<double, vector<int>> P;
const double Inf = 1e100;

int n, no[15], dis[15], box[15];
int w[1 << 15];

double dp[1 << 15][15];
vector<int> path[1 << 15][15];

double recur(int visited, int current) {
  if (dp[visited][current] != Inf)
    return dp[visited][current];

  double res = Inf;
  int next;
  for (int i = 0; i < n; ++i) {
    if (!(visited & 1 << i)) {
      double t = recur(visited | 1 << i, i) +
                 abs(dis[current] - dis[i]) / (2000.0 / (70.0 + w[visited]));
      if (t < res) {
        res = t;
        next = i;
      }
    }
  }
  path[visited][current] = path[visited | 1 << next][next];
  path[visited][current].push_back(no[current]);
  return res;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf("%d %d %d", no + i, dis + i, box + i);

  for (int i = 0; i < 1 << n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i & 1 << j)
        w[i] += 20 * box[j];
      dp[i][j] = Inf;
    }
  }
  for (int i = 0; i < n; ++i) {
    dp[(1 << n) - 1][i] = 0;
    path[(1 << n) - 1][i].push_back(no[i]);
  }

  double ans = Inf;
  int v;
  for (int i = 0; i < n - 1; ++i) {
    double t = recur(1 << i, i);
    if (t < ans) {
      ans = t;
      v = i;
    }
  }
  vector<int> &p = path[1 << v][v];
  for (int i = p.size() - 1; i > 0; --i)
    printf("%d ", p[i]);
  printf("%d\n", p[0]);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

typedef long long ll;
const double Eps = 1e-6;

using namespace std;

typedef pair<double, vector<int>> P;
const double Inf = 1e100;

int n, no[15], dis[15], box[15];
int w[1 << 15];

double dp[1 << 15][15];
vector<int> path[1 << 15][15];

double recur(int visited, int current) {
  if (dp[visited][current] != Inf)
    return dp[visited][current];

  double res = Inf;
  int next;
  for (int i = 0; i < n; ++i) {
    if (!(visited & 1 << i)) {
      double t = recur(visited | 1 << i, i) +
                 abs(dis[current] - dis[i]) / (2000.0 / (70.0 + w[visited]));
      if (t < res) {
        res = t;
        next = i;
      }
    }
  }
  path[visited][current] = path[visited | 1 << next][next];
  path[visited][current].push_back(no[current]);
  return dp[visited][current] = res;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf("%d %d %d", no + i, dis + i, box + i);

  for (int i = 0; i < 1 << n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i & 1 << j)
        w[i] += 20 * box[j];
      dp[i][j] = Inf;
    }
  }
  for (int i = 0; i < n; ++i) {
    dp[(1 << n) - 1][i] = 0;
    path[(1 << n) - 1][i].push_back(no[i]);
  }

  double ans = Inf;
  int v;
  for (int i = 0; i < n - 1; ++i) {
    double t = recur(1 << i, i);
    if (t < ans) {
      ans = t;
      v = i;
    }
  }
  vector<int> &p = path[1 << v][v];
  for (int i = p.size() - 1; i > 0; --i)
    printf("%d ", p[i]);
  printf("%d\n", p[0]);

  return 0;
}