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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, f, n) for (ll i = (f); i < (n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define repv(v) for (auto it = (v).begin(); it != (v).end(); it++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define INIT                                                                   \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false);

template <class T> inline bool chmax(T &a, T b) { return a = (a < b) ? b : a; }
template <class T> inline bool chmin(T &a, T b) { return a = (a > b) ? b : a; }

ll const INF = 1LL << 60;
ll const MOD = 1000000007;

ll const MAX_C = 10005;

int main() {
  INIT;
  ll N;
  cin >> N;
  vector<ll> X(N), Y(N), P(N);
  rep(i, N) { cin >> X[i] >> Y[i] >> P[i]; }

  ll pow_N = 1 << N;
  vector<vector<ll>> cost_x(N, vector<ll>(pow_N));
  vector<vector<ll>> cost_y(N, vector<ll>(pow_N));

  for (ll bit = 0; bit < pow_N; bit++) {
    rep(i, N) {
      ll min_x = INF;
      ll min_y = INF;

      chmin(min_x, abs(X[i]) * P[i]);
      chmin(min_y, abs(Y[i]) * P[i]);

      for (ll j = 0; j < N; j++) {
        if (bit & (1 << j)) {
          chmin(min_x, abs(X[i] - X[j]) * P[i]);
          chmin(min_y, abs(Y[i] - Y[j]) * P[i]);
        }
      }

      cost_x[i][bit] = min_x;
      cost_y[i][bit] = min_y;
    }
  }

  vector<ll> min_sum(N + 1, INF);
  vector<ll> ids;
  vector<pair<ll, ll>> now_lines;

  // 3進数bit全探索
  for (ll bit = 0; bit < pow(3, N); bit++) {
    int tmp = bit;
    for (int pos = 1; pos <= N; pos++) {
      ids.pb(tmp % 3);
      tmp /= 3;
    }

    ll id_x = 0, id_y = 0;
    ll k_id = 0;
    rep(i, N) {
      if (ids[i] == 0) {
        id_x += 1 << i;
        k_id++;
      } else if (ids[i] == 1) {
        id_y += 1 << i;
        k_id++;
      }
    }

    ll sum = 0;
    rep(i, N) { sum += min(cost_x[i][id_x], cost_y[i][id_y]); }
    chmin(min_sum[k_id], sum);

    ids.clear();
    now_lines.clear();
  }

  rep(i, N + 1) { cout << min_sum[i] << endl; }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, f, n) for (ll i = (f); i < (n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define repv(v) for (auto it = (v).begin(); it != (v).end(); it++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define INIT                                                                   \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false);

template <class T> inline bool chmax(T &a, T b) { return a = (a < b) ? b : a; }
template <class T> inline bool chmin(T &a, T b) { return a = (a > b) ? b : a; }

ll const INF = 1LL << 60;
ll const MOD = 1000000007;

ll const MAX_C = 10005;

int main() {
  INIT;
  ll N;
  cin >> N;
  vector<ll> X(N), Y(N), P(N);
  rep(i, N) { cin >> X[i] >> Y[i] >> P[i]; }

  vector<pair<ll, ll>> line;
  // line.pb({0, 0});
  // line.pb({1, 0});
  rep(i, N) {
    line.pb({0, X[i]});
    line.pb({1, Y[i]});
  }

  vector<ll> min_sum(N + 1, INF);
  vector<ll> ids;
  vector<pair<ll, ll>> now_lines;

  // 3進数bit全探索
  for (ll bit = 0; bit < pow(3, N); bit++) {
    int tmp = bit;
    for (int pos = 1; pos <= N; pos++) {
      ids.pb(tmp % 3);
      // cout << tmp % 3 + 1 << " ";
      tmp /= 3;
    }

    now_lines.pb({0, 0});
    now_lines.pb({1, 0});

    rep(i, SZ(ids)) {
      if (ids[i] == 0) {
        now_lines.pb(line[2 * i]);
      } else if (ids[i] == 1) {
        now_lines.pb(line[2 * i + 1]);
      }
    }

    // rep(k, SZ(now_lines)) {
    //     cout << "(" << now_lines[k].first << ", " << now_lines[k].second
    //          << "), ";
    // }
    // cout << endl;

    ll sum = 0;
    ll k_id = SZ(now_lines) - 2;

    rep(j, N) {
      ll minj = INF;

      rep(k, SZ(now_lines)) {
        if (now_lines[k].first == 0) {
          chmin(minj, abs(X[j] - now_lines[k].second) * P[j]);
        } else if (now_lines[k].first == 1) {
          chmin(minj, abs(Y[j] - now_lines[k].second) * P[j]);
        }
      }
      sum += minj;
    }

    chmin(min_sum[k_id], sum);

    ids.clear();
    now_lines.clear();
  }

  rep(i, N + 1) { cout << min_sum[i] << endl; }

  return 0;
}