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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

/*
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
*/

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long long, long long> pll;
typedef vector<pll> vpll;

typedef long double ld;
typedef vector<ld> vld;

typedef vector<bool> vb;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, n) for (ll i = (n); i >= 1; i--)
#define all(v) (v).begin(), (v).end()

template <class T> void chmin(T &a, T b) { a = min(a, b); }
template <class T> void chmax(T &a, T b) { a = max(a, b); }

constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);

void solve() {
  ll n;
  cin >> n;
  vll x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];

  vvll distx(n, vll(1 << n, INFL)), disty(n, vll(1 << n, INFL));
  for (ll mask = 0; mask < (1 << n); mask++) {
    for (ll i = 0; i < n; i++) {
      chmin(distx[i][mask], abs(x[i]));
      chmin(disty[i][mask], abs(y[i]));
      for (ll j = 0; j < n; j++) {
        if (mask & (1 << j)) {
          chmin(distx[i][mask], abs(x[i] - x[j]));
          chmin(disty[i][mask], abs(y[i] - y[j]));
        }
      }
    }
  }

  ll pow3 = 1;
  rep(i, n) pow3 *= 3;
  vll ans(n + 1, INFL);
  for (ll i = 0; i < pow3; i++) {
    ll cnt_road = 0;
    ll mask = i;
    ll maskx = 0, masky = 0;
    for (ll town = 0; town < n; town++) {
      if (mask % 3 == 1) {
        cnt_road++;
        maskx |= (1 << town);
      } else if (mask % 3 == 2) {
        cnt_road++;
        masky |= (1 << town);
      }
      mask /= 3;
    }
    ll sum = 0;
    for (ll town = 0; town < n; town++) {
      ll path = min(distx[town][maskx], disty[town][masky]);
      sum += path * p[town];
    }
    chmin(ans[cnt_road], sum);
  }
  for (auto &i : ans)
    cout << i << endl;
  return;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

/*
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
*/

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long long, long long> pll;
typedef vector<pll> vpll;

typedef long double ld;
typedef vector<ld> vld;

typedef vector<bool> vb;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, n) for (ll i = (n); i >= 1; i--)
#define all(v) (v).begin(), (v).end()

template <class T> void chmin(T &a, T b) { a = min(a, b); }
template <class T> void chmax(T &a, T b) { a = max(a, b); }

constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);

void solve() {
  ll n;
  cin >> n;
  vll x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  ll pow3 = 1;
  rep(i, n) pow3 *= 3;
  vll ans(n + 1, INFL);
  for (ll i = 0; i < pow3; i++) {
    ll cnt_road = 0;
    ll mask = i;
    vll roadx(1), roady(1);
    for (ll town = 0; town < n; town++) {
      if (mask % 3 == 1) {
        cnt_road++;
        roadx.push_back(x[town]);
      } else if (mask % 3 == 2) {
        cnt_road++;
        roady.push_back(y[town]);
      }
      mask /= 3;
    }
    sort(roadx.begin(), roadx.end());
    sort(roady.begin(), roady.end());
    ll sum = 0;
    for (ll town = 0; town < n; town++) {
      ll path = INFL;
      ll xn = roadx.size();
      ll xi = lower_bound(roadx.begin(), roadx.end(), x[town]) - roadx.begin();
      if (xi > 0)
        chmin(path, x[town] - roadx[xi - 1]);
      if (xi < xn)
        chmin(path, roadx[xi] - x[town]);
      ll yn = roady.size();
      ll yi = lower_bound(roady.begin(), roady.end(), y[town]) - roady.begin();
      if (yi > 0)
        chmin(path, y[town] - roady[yi - 1]);
      if (yi < yn)
        chmin(path, roady[yi] - y[town]);
      sum += path * p[town];
    }
    chmin(ans[cnt_road], sum);
  }
  for (auto &i : ans)
    cout << i << endl;
  return;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
}