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

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

// マクロ&定数&関数 ================================================
typedef unsigned int uint;
typedef long long ll;

typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<double> vdouble;
typedef vector<bool> vbool;
typedef vector<string> vstring;

typedef vector<pair<int, int>> vpint;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<double, double>> vpdouble;

typedef vector<vector<int>> vvint;
typedef vector<vector<ll>> vvll;
typedef vector<vpint> vvpint;
typedef vector<vpll> vvpll;
typedef vector<vector<double>> vvdouble;
typedef vector<vector<string>> vvstring;
typedef vector<vector<bool>> vvbool;

typedef vector<vector<vector<ll>>> vvvll;

const int INF = 1e9 + 1;
const ll LLINF = 1e17 + 1;
const int DX[9] = {0, 0, 1, -1, 1, 1, -1, -1, 0}; // 4;４近傍
const int DY[9] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; // 8:８近傍 9:(0,0)を含む
const double PI = 3.14159265358979323846264338327950288;
const ll MOD = 1000000007; // 10^9 + 7

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

vint to_bitlist(ll bit, ll fixed_size = 1) {
  if (bit == 0)
    return vint(fixed_size, 0);
  vint list;
  while (bit > 0) {
    list.emplace_back(bit % 3);
    bit /= 3;
  }
  while (list.size() < fixed_size) {
    list.emplace_back(0);
  }
  return list;
}

vint two_list(ll bit, ll fixed_size = 1) {
  if (bit == 0)
    return vint(fixed_size, 0);
  vint list;
  while (bit > 0) {
    list.emplace_back(bit % 2);
    bit /= 2;
  }
  while (list.size() < fixed_size) {
    list.emplace_back(0);
  }
  return list;
}

int main() {

  //////==================================
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(5);
  //////==================================

  /*
  ～思いついたことはとりあえず絶対メモする！！～



  */

  ll N;
  cin >> N;

  vll X(N), Y(N), P(N);
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> P[i];
  }

  // 前処理
  vvll disX(1 << N, vll(N)); // xがi組目の時の各X
  for (ll bit = 0; bit < 1 << N; bit++) {
    vint list = two_list(bit, N);

    for (int i = 0; i < N; i++) {
      disX[bit][i] = abs(X[i]);
      for (int j = 0; j < N; j++) {
        if (list[j])
          chmin(disX[bit][i], abs(X[i] - X[j]));
      }
    }
  }

  vvll disY(1 << N, vll(N)); // xがi組目の時の各Y
  for (ll bit = 0; bit < 1 << N; bit++) {
    vint list = two_list(bit, N);

    for (int i = 0; i < N; i++) {
      disY[bit][i] = abs(Y[i]);
      for (int j = 0; j < N; j++) {
        if (list[j])
          chmin(disY[bit][i], abs(Y[i] - Y[j]));
      }
    }
  }

  vll ans(N + 1, LLINF);
  vint list(N);
  for (ll bit = 0; bit < pow(3, N); bit++) {

    for (int i = 0; i < N; i++) {
      list[i] = 0;
    }

    ll b = bit;
    ll pi = 0;
    if (b != 0) {
      while (b > 0) {
        list[pi] = b % 3;
        b /= 3;
        pi++;
      }
    }

    // for (int i = 0; i < N; i++) cout << list[i] << " ";
    // cout << endl;

    // 0: none, 1: horizon, 2: ver
    ll count = 0;
    for (int i = 0; i < N; i++) {
      count += bool(list[i]);
    }

    ll bitX = 0;
    ll bitY = 0;
    for (int i = 0; i < N; i++) {
      if (list[i] == 1)
        bitX += 1 << i;
      if (list[i] == 2)
        bitY += 1 << i;
    }

    ll local = 0;
    for (int i = 0; i < N; i++) {
      local += min(disX[bitX][i], disY[bitY][i]) * P[i];
      ;
    }

    chmin(ans[count], local);
  }

  for (int i = 0; i <= N; i++)
    cout << ans[i] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

// マクロ&定数&関数 ================================================
typedef unsigned int uint;
typedef long long ll;

typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<double> vdouble;
typedef vector<bool> vbool;
typedef vector<string> vstring;

typedef vector<pair<int, int>> vpint;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<double, double>> vpdouble;

typedef vector<vector<int>> vvint;
typedef vector<vector<ll>> vvll;
typedef vector<vpint> vvpint;
typedef vector<vpll> vvpll;
typedef vector<vector<double>> vvdouble;
typedef vector<vector<string>> vvstring;
typedef vector<vector<bool>> vvbool;

typedef vector<vector<vector<ll>>> vvvll;

const int INF = 1e9 + 1;
const ll LLINF = 1e17 + 1;
const int DX[9] = {0, 0, 1, -1, 1, 1, -1, -1, 0}; // 4;４近傍
const int DY[9] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; // 8:８近傍 9:(0,0)を含む
const double PI = 3.14159265358979323846264338327950288;
const ll MOD = 1000000007; // 10^9 + 7

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

vint to_bitlist(ll bit, ll fixed_size = 1) {
  if (bit == 0)
    return vint(fixed_size, 0);
  vint list;
  while (bit > 0) {
    list.emplace_back(bit % 3);
    bit /= 3;
  }
  while (list.size() < fixed_size) {
    list.emplace_back(0);
  }
  return list;
}

vint two_list(ll bit, ll fixed_size = 1) {
  if (bit == 0)
    return vint(fixed_size, 0);
  vint list;
  while (bit > 0) {
    list.emplace_back(bit % 2);
    bit /= 2;
  }
  while (list.size() < fixed_size) {
    list.emplace_back(0);
  }
  return list;
}

int main() {

  //////==================================
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(5);
  //////==================================

  /*
  ～思いついたことはとりあえず絶対メモする！！～



  */

  ll N;
  cin >> N;

  vll X(N), Y(N), P(N);
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> P[i];
  }

  // 前処理
  vvll disX(1 << N, vll(N)); // xがi組目の時の各X
  for (ll bit = 0; bit < 1 << N; bit++) {
    vint list = two_list(bit, N);

    for (int i = 0; i < N; i++) {
      disX[bit][i] = abs(X[i]);
      for (int j = 0; j < N; j++) {
        if (list[j])
          chmin(disX[bit][i], abs(X[i] - X[j]));
      }
    }
  }

  vvll disY(1 << N, vll(N)); // xがi組目の時の各Y
  for (ll bit = 0; bit < 1 << N; bit++) {
    vint list = two_list(bit, N);

    for (int i = 0; i < N; i++) {
      disY[bit][i] = abs(Y[i]);
      for (int j = 0; j < N; j++) {
        if (list[j])
          chmin(disY[bit][i], abs(Y[i] - Y[j]));
      }
    }
  }

  vll ans(N + 1, LLINF);
  vint list;
  ;
  for (ll bit = 0; bit < pow(3, N); bit++) {
    list = to_bitlist(bit, N);

    // 0: none, 1: horizon, 2: ver
    ll count = 0;
    for (int i = 0; i < N; i++) {
      count += bool(list[i]);
    }

    ll bitX = 0;
    ll bitY = 0;
    for (int i = 0; i < N; i++) {
      if (list[i] == 1)
        bitX += 1 << i;
      if (list[i] == 2)
        bitY += 1 << i;
    }

    ll local = 0;
    for (int i = 0; i < N; i++) {
      local += min(disX[bitX][i], disY[bitY][i]) * P[i];
      ;
    }

    chmin(ans[count], local);
  }

  for (int i = 0; i <= N; i++)
    cout << ans[i] << endl;
}