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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = (0); i < (n); i++)

using namespace std;

typedef long long ll;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n;
vector<ll> x, y, p;
vector<ll> ans;

vector<vector<ll>> cx, cy;

void dfs(int k, int d, vector<int> &w) {
  if (w.size() == n) {
    ll xb = 0, yb = 0;
    rep(i, n) {
      if (w[i] == 1)
        xb |= (1 << i);
      if (w[i] == 2)
        yb |= (1 << i);
    }

    ll t = 0;
    rep(i, n) { t += p[i] * min(cx[i][xb], cy[i][yb]); }
    chmin(ans[d], t);
    return;
  }

  w.push_back(0);
  dfs(k, d, w);
  w.pop_back();

  w.push_back(1);
  dfs(k, d + 1, w);
  w.pop_back();

  w.push_back(2);
  dfs(k, d + 1, w);
  w.pop_back();
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;

  x.resize(n);
  y.resize(n);
  p.resize(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];

  cx.resize(n, vector<ll>((1 << n), 0));
  cy.resize(n, vector<ll>((1 << n), 0));

  rep(i, n) {
    for (ll bit = 0; bit < (1 << n); bit++) {
      ll t = abs(x[i] - 0);
      rep(j, n) {
        if ((bit >> j) & 1)
          chmin(t, abs(x[i] - x[j]));
      }
      cx[i][bit] = t;
    }
  }

  rep(i, n) {
    for (ll bit = 0; bit < (1 << n); bit++) {
      ll t = abs(y[i] - 0);
      rep(j, n) {
        if ((bit >> j) & 1)
          chmin(t, abs(y[i] - y[j]));
      }
      cy[i][bit] = t;
    }
  }

  ans.resize(n + 1, 1e18);
  vector<int> w;
  dfs(0, 0, w);
  rep(i, n + 1) cout << ans[i] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = (0); i < (n); i++)

using namespace std;

typedef long long ll;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n;
vector<ll> x, y, p;
vector<ll> ans;

ll calc(vector<int> w) {
  vector<ll> v, h;
  v.push_back(0);
  h.push_back(0);

  v.push_back(-1e10);
  v.push_back(-1e10);
  h.push_back(-1e10);
  h.push_back(-1e10);
  v.push_back(1e10);
  v.push_back(1e10);
  h.push_back(1e10);
  h.push_back(1e10);

  vector<bool> conn(n, false);
  rep(i, n) {
    if (w[i] == 1) {
      v.push_back(x[i]);
      conn[i] = true;
    }
    if (w[i] == 2) {
      h.push_back(y[i]);
      conn[i] = true;
    }
  }

  sort(v.begin(), v.end());
  sort(h.begin(), h.end());

  ll ret = 0;
  rep(i, n) {
    if (conn[i])
      continue;

    ll t = 1e18;
    auto itr = lower_bound(v.begin(), v.end(), x[i]);
    itr--;
    chmin(t, abs(x[i] - *itr));
    itr++;
    chmin(t, abs(x[i] - *itr));

    itr = lower_bound(h.begin(), h.end(), y[i]);
    itr--;
    chmin(t, abs(y[i] - *itr));
    itr++;
    chmin(t, abs(y[i] - *itr));

    ret += t * p[i];
  }

  return ret;
}

void dfs(int k, int d, vector<int> &w) {
  if (w.size() == n) {
    if (d < n)
      chmin(ans[d], calc(w));
    return;
  }

  w.push_back(0);
  dfs(k, d, w);

  w.pop_back();
  w.push_back(1);
  dfs(k, d + 1, w);

  w.pop_back();
  w.push_back(2);
  dfs(k, d + 1, w);

  w.pop_back();
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;

  x.resize(n);
  y.resize(n);
  p.resize(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];

  ans.resize(n + 1, 1e18);
  ans[n] = 0;
  vector<int> w;
  dfs(0, 0, w);
  rep(i, n + 1) cout << ans[i] << endl;
}