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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)
// typedef long long ll;

using namespace std;

const int INF = 1000000000;
const int N = 16;
const int MAX = 1 << N;

pair<double, int> dp[N][MAX];

int n;
int id[N];
int dis_c[N];
int weight[N];
int w[MAX];
int dist[N][N];

int main(void) {

  int shift = 0;

  rep(i, N) rep(j, MAX) dp[i][j] = pair<double, int>(INF, INF);
  rep(i, N) rep(j, N) dist[i][j] = INF;
  rep(i, N) dist[i][i] = 0;

  scanf("%d", &n);

  rep(i, n) { scanf("%d %d %d", &id[i], &dis_c[i], &weight[i]); }

  shift = 1 << n;

  rep(i, shift) {
    int sum = 0;
    rep(j, n) {
      if (i & 1 << j) {
        sum += weight[j];
      }
    }
    w[i] = sum;
  }

  rep(i, n) rep(j, n) { dist[i][j] = dist[j][i] = abs(dis_c[i] - dis_c[j]); }

  rep(i, n) dp[i][1 << i] = pair<double, int>(0, -1);

  rep(i, shift) {
    rep(j, n) {
      if (i & (1 << j)) {
        rep(k, n) {
          if (!(i & (1 << k))) {
            if (k == j)
              continue;
            //						printf("dp[%d][%d].f = %lf, dist[%d][%d] = %d, w[%d] =
            //%d\\n", j,i,dp[j][i].first, k, i, dist[j][k], i, w[i]);
            if (dp[k][i | 1 << k].first > dp[j][i].first + dist[j][k] * w[i]) {
              dp[k][i | 1 << k].first = dp[j][i].first + dist[j][k] * w[i];
              dp[k][i | 1 << k].second = j;
            }
          }
        }
      }
    }
  }

  int ans = INF;
  int p = -1;
  int x = shift - 1;
  rep(i, n) {
    if (ans > dp[i][x].first) {
      ans = dp[i][x].first;
      p = i;
    }
  }

  stack<int> s;

  while (p != -1 && x != 0) {
    int n = dp[p][x].second;
    s.push(id[p]);
    x -= (1 << p);
    p = n;
  }

  while (!s.empty()) {
    printf("%d", s.top());
    s.pop();
    if (!s.empty())
      printf(" ");
  }
  puts("");

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

using namespace std;

const int INF = 1000000000;

int dis[16][16];
int n;
double anst;
int ans[20];
int temp[20];
int z[16];

void dfs(int s, int now, double t, int w, int cnt) {
  if (cnt == n) {
    if (anst > t) {
      //			printf("%lf > %lf\\n", anst, t);
      rep(i, n) {
        //				printf("%3d ", temp[i]);
        ans[i] = temp[i];
      }
      anst = t;
      //			puts("");
    }
    return;
  }
  rep(i, n) {
    if (!(s & (1 << i))) {

      temp[cnt] = i;
      dfs(s | (1 << i), i, t + (dis[now][i] / (2000 / (70. + w))), w + z[i],
          cnt + 1);
    }
  }
}

int main(void) {

  int id[16];
  int dis_c[16];

  scanf("%d", &n);

  rep(i, n) {
    scanf("%d %d %d", &id[i], &dis_c[i], &z[i]);
    z[i] *= 20;
  }
  anst = INF;
  rep(i, 16) rep(j, 16) dis[i][j] = INF;
  rep(i, n) rep(j, n) dis[i][j] = dis[j][i] = abs(dis_c[i] - dis_c[j]);

  rep(i, n) {
    temp[0] = i;
    dfs(1 << i, i, 0, z[i], 1);
  }
  printf("%d", id[ans[0]]);
  reps(i, 1, n) { printf(" %d", id[ans[i]]); }
  puts("");

  return 0;
}