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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)
#define lb(s, x) (lower_bound(all(s), x) - s.begin())
#define ub(s, x) (upper_bound(all(s), x) - s.begin())
#define uniq(a) a.erase(unique(all(a)), a.end())
#define bit(k) (1LL << (k))
#define INF ll(1e18)

template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &p) {
  ost << "(" << p.first << ", " << p.second << ")";
  return ost;
}
#define ostream_container                                                      \\
  {                                                                            \\
    ost << "{";                                                                \\
    for (const auto &t : v) {                                                  \\
      if (&t != &*v.begin())                                                   \\
        ost << ", ";                                                           \\
      ost << t;                                                                \\
    }                                                                          \\
    ost << "}";                                                                \\
    return ost;                                                                \\
  }
template <class T>
ostream &operator<<(ostream &ost, const vector<T> &v) ostream_container
    template <class T>
    ostream &operator<<(ostream &ost, const set<T> &v) ostream_container
    template <class T, class U>
    ostream &operator<<(ostream &ost, const map<T, U> &v) ostream_container

    template <class H>
    void recursive_debug(string s, H h) {
  cerr << "\\033[33m" << s << " = " << h << endl << "\\033[m";
}
template <class H, class... T> void recursive_debug(string s, H h, T... t) {
  int comma = s.find(',');
  cerr << "\\033[33m" << s.substr(0, comma) << " = " << h << ", ";
  recursive_debug(s.substr(comma + 1), t...);
}
#define debug(...) recursive_debug(#__VA_ARGS__, __VA_ARGS__)

struct Point {
  ll x, y, p;
  bool operator<(const Point &rhs) const {
    return x != rhs.x ? x < rhs.x : y < rhs.y;
  }
};

vector<int> get_state(int id, int n) {
  vector<int> state(n);
  rep(i, n) {
    state[i] = id % 3;
    id /= 3;
  }
  return state;
}

int main(void) {
  int n;
  cin >> n;
  vector<Point> v(n);
  rep(i, n) cin >> v[i].x >> v[i].y >> v[i].p;
  vector<vector<ll>> distX(1 << n, vector<ll>(n, INF)),
      distY(1 << n, vector<ll>(n, INF));
  rep(i, n) {
    distX[0][i] = abs(v[i].x);
    distY[0][i] = abs(v[i].y);
  }
  rep(mask, 1 << n) {
    rep(road, n) {
      if (mask & bit(road))
        continue;
      rep(i, n) {
        distX[mask | bit(road)][i] =
            min(distX[mask][i], abs(v[road].x - v[i].x));
        distY[mask | bit(road)][i] =
            min(distY[mask][i], abs(v[road].y - v[i].y));
      }
    }
  }
  vector<ll> res(n + 1, INF);
  rep(id, pow(3, n) + 0.5) {
    vector<int> state = get_state(id, n);
    int maskX = 0, maskY = 0;
    rep(i, n) {
      if (state[i] == 1)
        maskX |= bit(i);
      if (state[i] == 2)
        maskY |= bit(i);
    }
    int use = 0;
    ll sum = 0;
    rep(i, n) {
      if (state[i])
        use++;
      sum += min(distX[maskX][i], distY[maskY][i]) * v[i].p;
    }
    res[use] = min(res[use], sum);
  }
  rep(i, n + 1) { cout << res[i] << endl; }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)
#define lb(s, x) (lower_bound(all(s), x) - s.begin())
#define ub(s, x) (upper_bound(all(s), x) - s.begin())
#define uniq(a) a.erase(unique(all(a)), a.end())
#define bit(k) (1LL << (k))
#define INF int(1e9)

template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &p) {
  ost << "(" << p.first << ", " << p.second << ")";
  return ost;
}
#define ostream_container                                                      \\
  {                                                                            \\
    ost << "{";                                                                \\
    for (const auto &t : v) {                                                  \\
      if (&t != &*v.begin())                                                   \\
        ost << ", ";                                                           \\
      ost << t;                                                                \\
    }                                                                          \\
    ost << "}";                                                                \\
    return ost;                                                                \\
  }
template <class T>
ostream &operator<<(ostream &ost, const vector<T> &v) ostream_container
    template <class T>
    ostream &operator<<(ostream &ost, const set<T> &v) ostream_container
    template <class T, class U>
    ostream &operator<<(ostream &ost, const map<T, U> &v) ostream_container

    template <class H>
    void recursive_debug(string s, H h) {
  cerr << "\\033[33m" << s << " = " << h << endl << "\\033[m";
}
template <class H, class... T> void recursive_debug(string s, H h, T... t) {
  int comma = s.find(',');
  cerr << "\\033[33m" << s.substr(0, comma) << " = " << h << ", ";
  recursive_debug(s.substr(comma + 1), t...);
}
#define debug(...) recursive_debug(#__VA_ARGS__, __VA_ARGS__)

struct Point {
  ll x, y, p;
  bool operator<(const Point &rhs) const {
    return x != rhs.x ? x < rhs.x : y < rhs.y;
  }
};

vector<int> get_state(int id, int n) {
  vector<int> state(n);
  rep(i, n) {
    state[i] = id % 3;
    id /= 3;
  }
  return state;
}

int main(void) {
  int n;
  cin >> n;
  vector<Point> v(n);
  rep(i, n) { cin >> v[i].x >> v[i].y >> v[i].p; }
  vector<ll> res(n + 1, 1e18);
  rep(id, pow(3, n) + 0.5) {
    vector<int> state = get_state(id, n);
    vector<ll> score(n);
    rep(i, n) { score[i] = min(abs(v[i].x), abs(v[i].y)) * v[i].p; }
    int use = 0;
    rep(road, n) {
      if (!state[road])
        continue;
      use++;
      rep(i, n) {
        if (state[road] == 1)
          score[i] = min(score[i], abs(v[road].x - v[i].x) * v[i].p);
        if (state[road] == 2)
          score[i] = min(score[i], abs(v[road].y - v[i].y) * v[i].p);
      }
    }
    res[use] = min(res[use], accumulate(all(score), 0LL));
  }
  rep(i, n + 1) { cout << res[i] << endl; }
  return 0;
}