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

#include <bits/stdc++.h>

using namespace std;
using int64 = long long;

constexpr int DEBUG = 0;

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

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

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

  int n;
  cin >> n;
  vector<int64> xs(n), ys(n), ps(n);
  for (int i = 0; i < n; i++) {
    cin >> xs[i] >> ys[i] >> ps[i];
  }

  vector<int64> ans(n + 1, INT64_MAX);
  for (int city_pattern = 0; city_pattern < 1 << n; city_pattern++) {
    vector<int> cs;
    for (int c = 0; c < n; c++) {
      if (city_pattern & (1 << c)) {
        cs.push_back(c);
      }
    }

    int num_roads = cs.size();

    vector<int64> r_xs;
    vector<int64> r_ys;
    for (int d_pattern = 0; d_pattern < 1 << num_roads; d_pattern++) {
      r_xs.clear();
      r_ys.clear();
      r_xs.push_back(0);
      r_ys.push_back(0);

      for (int r = 0; r < num_roads; r++) {
        if (d_pattern & (1 << r)) {
          r_ys.push_back(ys[cs[r]]);
        } else {
          r_xs.push_back(xs[cs[r]]);
        }
      }

      int64 cost = 0;
      for (int c = 0; c < n; c++) {
        if (city_pattern & (1 << c))
          continue;
        int64 min_d = INT64_MAX;
        for (int64 r_x : r_xs) {
          UpdateMin(min_d, abs(xs[c] - r_x));
        }
        for (int64 r_y : r_ys) {
          UpdateMin(min_d, abs(ys[c] - r_y));
        }
        cost += min_d * ps[c];
      }

      UpdateMin(ans[num_roads], cost);
    }
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using int64 = long long;

constexpr int DEBUG = 0;

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

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

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

  int n;
  cin >> n;
  vector<int64> xs(n), ys(n), ps(n);
  for (int i = 0; i < n; i++) {
    cin >> xs[i] >> ys[i] >> ps[i];
  }

  vector<int64> ans(n + 1, INT64_MAX);
  for (int city_pattern = 0; city_pattern < 1 << n; city_pattern++) {
    vector<int> cs;
    for (int c = 0; c < n; c++) {
      if (city_pattern & (1 << c)) {
        cs.push_back(c);
      }
    }

    int num_roads = cs.size();

    for (int d_pattern = 0; d_pattern < 1 << num_roads; d_pattern++) {
      vector<int64> r_xs;
      vector<int64> r_ys;
      r_xs.push_back(0);
      r_ys.push_back(0);

      for (int r = 0; r < num_roads; r++) {
        if (d_pattern & (1 << r)) {
          r_ys.push_back(ys[cs[r]]);
        } else {
          r_xs.push_back(xs[cs[r]]);
        }
      }

      int64 cost = 0;
      for (int c = 0; c < n; c++) {
        if (city_pattern & (1 << c))
          continue;
        int64 min_d = INT64_MAX;
        for (int64 r_x : r_xs) {
          UpdateMin(min_d, abs(xs[c] - r_x));
        }
        for (int64 r_y : r_ys) {
          UpdateMin(min_d, abs(ys[c] - r_y));
        }
        cost += min_d * ps[c];
      }

      UpdateMin(ans[num_roads], cost);
    }
  }

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