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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <x86intrin.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
vector<int> dx = {-1, -1, -1, 0, 0, 1, 1, 1};
vector<int> dy = {-1, 0, 1, -1, 1, -1, 0, 1};

const ll INF = 1e18;

int pow2(int x) {
  int res = 1;
  while (x--)
    res *= 2;
  return res;
}

int main() {
  int n;
  cin >> n;
  vector<ll> x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];

  vector<vector<ll>> dist_x(n, vector<ll>(n)), dist_y(n, vector<ll>(n));
  rep(i, n) rep(j, n) {
    dist_x[i][j] = dist_x[j][i] = abs(x[i] - x[j]);
    dist_y[i][j] = dist_y[j][i] = abs(y[i] - y[j]);
  }

  vector<vector<ll>> ver(1 << n, vector<ll>(n)), hor(1 << n, vector<ll>(n));
  rep(i, 1 << n) {
    vector<int> c; // 路線通す都市
    rep(j, n) {
      if ((i >> j) & 1)
        c.push_back(j);
    }
    rep(j, n) {
      {
        ll cost = min(abs(x[j]) * p[j], abs(y[j]) * p[j]);
        for (int q : c) {
          cost = min(cost, abs((x[j] - x[q]) * p[j]));
        }
        ver[i][j] = cost;
      }
      {
        ll cost = min(abs(x[j]) * p[j], abs(y[j]) * p[j]);
        for (int q : c) {
          cost = min(cost, abs((y[j] - y[q]) * p[j]));
        }
        hor[i][j] = cost;
      }
    }
  }

  vector<ll> ans(n + 1, INF);
  vector<ll> zero(n);
  rep(i, n) zero[i] = min(abs(x[i]), abs(y[i]));
  for (int i = 0; i < (1 << n); i++) {
    int k = _popcnt32(i);
    vector<int> city;
    rep(j, n) {
      if ((i >> j) & 1)
        city.push_back(j);
    }
    for (int j = 0; j < (1 << k); j++) {
      int _ver = 0, _hor = 0;
      rep(l, k) {
        if ((j >> l) & 1)
          _ver += pow2(city[l]);
        else
          _hor += pow2(city[l]);
      }
      ll cost = 0;
      rep(l, n) { cost += min(ver[_ver][l], hor[_hor][l]); }
      ans[k] = min(ans[k], cost);
      /*
      vector<ll> cnt = zero;
      rep(l, n) {
          rep(m, k) {
              if ((j >> m) & 1) cnt[l] = min(cnt[l], dist_x[l][city[m]]);
              else cnt[l] = min(cnt[l], dist_y[l][city[m]]);
          }
          cnt[l] *= p[l];
      }
      ll s = 0;
      rep(l, n) s += cnt[l];
      ans[k] = min(ans[k], s);
      */
    }
  }
  for (int k = 0; k <= n; k++)
    cout << ans[k] << endl;
  return 0;
}

Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <x86intrin.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
vector<int> dx = {-1, -1, -1, 0, 0, 1, 1, 1};
vector<int> dy = {-1, 0, 1, -1, 1, -1, 0, 1};

const ll INF = 1e18;

int main() {
  int n;
  cin >> n;
  vector<ll> x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  vector<vector<ll>> dist_x(n, vector<ll>(n)), dist_y(n, vector<ll>(n));
  rep(i, n) rep(j, n) {
    dist_x[i][j] = dist_x[j][i] = abs(x[i] - x[j]);
    dist_y[i][j] = dist_y[j][i] = abs(y[i] - y[j]);
  }
  vector<ll> ans(n + 1, INF);
  vector<ll> zero(n);
  rep(i, n) zero[i] = min(abs(x[i]), abs(y[i]));
  for (int i = 0; i < (1 << n); i++) {
    int k = _popcnt32(i);
    vector<int> city;
    rep(j, n) {
      if ((i >> j) & 1)
        city.push_back(j);
    }
    for (int j = 0; j < (1 << k); j++) {
      vector<ll> cnt = zero;
      rep(l, n) {
        rep(m, k) {
          if ((j >> m) & 1)
            cnt[l] = min(cnt[l], dist_x[l][city[m]]);
          else
            cnt[l] = min(cnt[l], dist_y[l][city[m]]);
        }
        cnt[l] *= p[l];
      }
      ll s = 0;
      rep(l, n) s += cnt[l];
      ans[k] = min(ans[k], s);
    }
  }
  for (int k = 0; k <= n; k++)
    cout << ans[k] << endl;
  return 0;
}