Problem p02604 (C++) — buggy submission, verdict: Runtime Error


#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define eb emplace_back
#define el '\\n'
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<string> vs;

ll inf = (ll)2e18;
const int mod = 1000000007;
// const int mod=998244353;

int n, m;
const int N = 2e5 + 10;

ll ho[1 << 15][15], ver[1 << 15][15];
ll dp[1 << 15];

struct node {
  ll x, y;
  ll p;
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int q, t, k, u, v;
  ll x, a, b, c, d, y, p, z;
  cin >> n;
  vector<node> vv(n);
  for (int i = 0; i < n; i++) {
    cin >> vv[i].x >> vv[i].y >> vv[i].p;
  }
  for (int i = 0; i < 1 << n; i++) {
    for (int j = 0; j < n; j++) {
      ho[i][j] = abs(vv[j].y);
      ver[i][j] = abs(vv[j].x);
    }
    for (int j = 0; j < n; j++) {
      if (i >> j & 1) {
        for (int k = 0; k < n; k++) {
          ho[i][k] = min(ho[i][k], abs(vv[k].y - vv[j].y));
          ver[i][k] = min(ver[i][k], abs(vv[k].x - vv[j].x));
        }
      }
    }
  }
  vll ans(16, inf);
  for (int i = 0; i < 1 << n; i++) {
    for (int j = i; j >= 0; j = (j - 1) & i) {
      int p = j;
      vll mn(15, inf);
      for (int k = 0; k < n; k++) {
        mn[k] = min({mn[k], ho[p][k], ver[i ^ p][k]});
      }
      a = 0;
      for (int k = 0; k < n; k++) {
        a += vv[k].p * mn[k];
      }
      ans[__builtin_popcount(i)] = min(ans[__builtin_popcount(i)], a);
      if (!j) {
        break;
      }
    }
  }
  for (int i = 0; i <= n; i++) {
    cout << ans[i] << el;
  }
  return 0;
}


Error: WA

Fixed submission:


#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define eb emplace_back
#define el '\\n'
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<string> vs;

ll inf = (ll)2e18;
const int mod = 1000000007;
// const int mod=998244353;

int n, m;
const int N = 2e5 + 10;

ll ho[1 << 16][16], ver[1 << 16][16];
ll dp[1 << 16];

struct node {
  ll x, y;
  ll p;
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int q, t, k, u, v;
  ll x, a, b, c, d, y, p, z;
  cin >> n;
  vector<node> vv(16);
  for (int i = 0; i < n; i++) {
    cin >> vv[i].x >> vv[i].y >> vv[i].p;
  }
  for (int i = 0; i < 1 << n; i++) {
    for (int j = 0; j < n; j++) {
      ho[i][j] = abs(vv[j].y);
      ver[i][j] = abs(vv[j].x);
    }
    for (int j = 0; j < n; j++) {
      if (i >> j & 1) {
        for (int k = 0; k < n; k++) {
          ho[i][k] = min(ho[i][k], abs(vv[k].y - vv[j].y));
          ver[i][k] = min(ver[i][k], abs(vv[k].x - vv[j].x));
        }
      }
    }
  }
  vll ans(15, inf);
  for (int i = 0; i < 1 << n; i++) {
    for (int j = i; j >= 0; j = (j - 1) & i) {
      int p = j;
      vll mn(15, inf);
      for (int k = 0; k < n; k++) {
        mn[k] = min({mn[k], ho[p][k], ver[i ^ p][k]});
      }
      a = 0;
      for (int k = 0; k < n; k++) {
        a += vv[k].p * mn[k];
      }
      ans[__builtin_popcount(i)] = min(ans[__builtin_popcount(i)], a);
      if (!j) {
        break;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] << ' ';
  }
  cout << 0 << el;
  return 0;
}