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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define Endl endl
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define over(A)                                                                \\
  {                                                                            \\
    cout << A << endl;                                                         \\
    exit(0);                                                                   \\
  }
#define all(A) A.begin(), A.end()
#define ceil(a, b) ((a - 1) / b + 1)
#define quickcin ios_base::sync_with_stdio(false);
template <typename T1, typename T2> inline void chmin(T1 &x, T2 b) {
  if (b < x)
    x = b;
}
template <typename T1, typename T2> inline void chmax(T1 &x, T2 b) {
  if (b > x)
    x = b;
}
template <typename T1, typename T2> inline void chmod(T1 &x, T2 b) {
  x %= b, x += b;
  if (x >= b)
    x -= b;
}
const int inf = 1039074182;
const ll llinf = 1LL * inf * inf;
using namespace std;
int n;
vector<vector<vector<ll>>> res(64);
vector<ull> S, T, U, V;
bool have0h[505];
bool have0l[505];
bool have1h[505];
bool have1l[505];

pair<bool, bool> solvesingle(int a, int b) {
  if (a == 0 && b == 1 || a == 1 && b == 0)
    return {false, false};
  int v;
  if (a != -1)
    v = a;
  if (b != -1)
    v = b;
  return {true, v};
}

pair<bool, vector<vector<bool>>> solve11(vector<int> h, vector<int> l) {
  pair<bool, bool> o = solvesingle(h[0], l[0]);
  return {o.first, {{o.second}}};
}

pair<bool, vector<vector<bool>>> solve1m(vector<int> h, vector<int> l) {
  int m = l.size();
  vector<bool> v;
  bool sa = false;
  if (h[0] == -1)
    sa = true;
  for (int i = 0; i < m; i++) {
    if (l[i] == -1 || l[i] == h[0]) {
      v.push_back(h[0]);
      sa = true;
    } else {
      v.push_back(l[i]);
    }
  }
  if (sa == false)
    return {false, {{}}};
  for (int i = 0; i < m; i++) {
    if (v[i] == -1)
      v[i] = 0;
  }
  //	cout<<l[0]<<' '<<l[1]<<' '<<h[0]<<endl;
  return {true, {v}};
}

using namespace std;
template <typename T> ostream &operator<<(ostream &cout, vector<T> vec) {
  cout << "{";
  for (int i = 0; i < vec.size(); i++) {
    cout << vec[i];
    if (i != (int)vec.size() - 1)
      cout << ',';
  }
  cout << "}";
  return cout;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &cout, pair<T1, T2> p) {
  cout << "(" << p.first << ',' << p.second << ")";
  return cout;
}

template <typename T> T operator*(vector<T> v1, vector<T> v2) {
  assert(v1.size() == v2.size());
  int n = v1.size();
  T res = 0;
  for (int i = 0; i < n; i++) {
    res += v1[i] * v2[i];
  }
  return res;
}

template <typename T1, typename T2> pair<T1, T2> operator-(pair<T1, T2> x) {
  return make_pair(-x.first, -x.second);
}

template <typename T> vector<vector<T>> operator~(vector<vector<T>> vec) {
  vector<vector<T>> v;
  int n = vec.size(), m = vec[0].size();
  v.resize(m);
  for (int i = 0; i < m; i++) {
    v[i].resize(n);
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      v[i][j] = vec[j][i];
    }
  }
  return v;
}
pair<bool, vector<vector<bool>>> solven1(vector<int> h, vector<int> l) {
  pair<bool, vector<vector<bool>>> o = solve1m(l, h);
  if (o.first == false)
    return {false, {{}}};
  else
    return mp(true, ~o.second);
}

pair<bool, vector<vector<bool>>> solvenm(vector<int> h, vector<int> l) {
  int n = h.size();
  int m = l.size();
  vector<vector<bool>> res(n);
  for (int i = 0; i < n; i++) {
    res[i].resize(m);
  }
  for (int i = 0; i < n; i++) {
    res[i][i % m] = 1;
  }
  for (int j = 0; j < m; j++) {
    res[j % n][j] = 1;
  }
  return mp(true, res);
}

pair<bool, vector<vector<bool>>> solve(vector<int> h, vector<int> l) {
  int n = h.size();
  int m = l.size();
  if (n == 0 || m == 0) {
    bool ok = true;
    for (auto &x : h)
      ok &= (x == -1);
    for (auto &y : l)
      ok &= (y == -1);
    return {ok, {{}}};
  }
  if (n == 1 && m == 1)
    return solve11(h, l);
  if (n == 1)
    return solve1m(h, l);
  if (m == 1)
    return solven1(h, l);
  return solvenm(h, l);
}

inline bool getbit(ull mask, int k) { return ((1uLL << k) & mask); }

inline void paint(int x, int y, int color) {
  if (color == 0)
    have0h[x] = true, have0l[y] = true;
  if (color == 1)
    have1h[x] = true, have1l[y] = true;
}

pair<bool, vector<vector<ll>>> solve(int k) {
  memset(have0h, 0, sizeof(have0h));
  memset(have0l, 0, sizeof(have0l));
  memset(have1h, 0, sizeof(have1h));
  memset(have1l, 0, sizeof(have1l));
  vector<vector<ll>> a(n, vector<ll>(n, -1));
  vector<int> h, l;
  vector<int> idh, idl;
  for (int i = 0; i < n; i++) {
    if (S[i] == 0 && getbit(U[i], k)) {
      for (int j = 0; j < n; j++)
        paint(i, j, 1);
    }
    if (S[i] == 1 && !getbit(U[i], k)) {
      for (int j = 0; j < n; j++)
        paint(i, j, 0);
    }
  }
  for (int j = 0; j < n; j++) {
    if (T[j] == 0 && getbit(V[j], k)) {
      for (int i = 0; i < n; i++) {
        paint(i, j, 1);
      }
    }
    if (T[j] == 1 && !getbit(V[j], k)) {
      for (int i = 0; i < n; i++) {
        paint(i, j, 0);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (S[i] == 0 && !getbit(U[i], k))
      h.push_back(have0h[i] ? -1 : 0), idh.push_back(i);
    if (S[i] == 1 && getbit(U[i], k))
      h.push_back(have1h[i] ? -1 : 1), idh.push_back(i);
  }
  for (int j = 0; j < n; j++) {
    if (T[j] == 0 && !getbit(V[j], k))
      l.push_back(have0l[j] ? -1 : 0), idl.push_back(j);
    if (T[j] == 1 && getbit(V[j], k))
      l.push_back(have1l[j] ? -1 : 1), idl.push_back(j);
  }
  pair<bool, vector<vector<bool>>> o = solve(h, l);
  if (o.first == false)
    return mp(false, a);
  vector<vector<bool>> &v = o.second;
  for (int i = 0; i < n; i++) {
    if (S[i] == 0 && getbit(U[i], k)) {
      for (int j = 0; j < n; j++) {
        if (a[i][j] != 0)
          a[i][j] = 1;
        else
          return mp(false, a);
      }
    }
    if (S[i] == 1 && !getbit(U[i], k)) {
      for (int j = 0; j < n; j++) {
        if (a[i][j] != 1)
          a[i][j] = 0;
        else
          return mp(false, a);
      }
    }
  }
  for (int j = 0; j < n; j++) {
    if (T[j] == 0 && getbit(V[j], k)) {
      for (int i = 0; i < n; i++) {
        if (a[i][j] != 0)
          a[i][j] = 1;
        else
          return mp(false, a);
      }
    }
    if (T[j] == 1 && !getbit(V[j], k)) {
      for (int i = 0; i < n; i++) {
        if (a[i][j] != 1)
          a[i][j] = 0;
        else
          return mp(false, a);
      }
    }
  }
  for (int i = 0; i < h.size(); i++) {
    for (int j = 0; j < l.size(); j++) {
      a[idh[i]][idl[j]] = v[i][j];
    }
  }
  return mp(true, a);
}

int main() {
  //	freopen("input.txt","r",stdin);
  //	freopen("output.txt","w",stdout);
  quickcin;
  cin >> n;
  S.resize(n);
  T.resize(n);
  U.resize(n);
  V.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> T[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> U[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> V[i];
  }
  for (int i = 0; i < 64; i++) {
    pair<bool, vector<vector<ll>>> o = solve(i);
    if (o.first == false)
      over(-1);
    res[i] = o.second;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      unsigned ll t = 0;
      for (int k = 0; k < 64; k++) {
        t |= ((unsigned long long)res[k][i][j] << k);
      }
      cout << t << ' ';
    }
    cout << endl;
  }
  return 0;
}

/*
2
1 1
0 0
1 1
0 1
*/


Error: WA

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = b - 1; i >= a; i--)
#define ALL(a) a.begin(), a.end()
using pii = pair<int, int>;
using piii = pair<pii, int>;
using pll = pair<long long, long long>;
using plll = pair<pll, long long>;
// #pragma GCC optimize("Ofast")
#define pcnt __builtin_popcount
#define buli(x) __builtin_popcountll(x)
#define pb push_back
#define mp make_pair
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define isSquare(x) (sqrt(x) * sqrt(x) == x)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
};
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
};
inline void in(void) { return; }
template <typename First, typename... Rest>
void in(First &first, Rest &...rest) {
  cin >> first;
  in(rest...);
  return;
}
inline void out(void) {
  cout << "\\n";
  return;
}
template <typename First, typename... Rest>
void out(First first, Rest... rest) {
  cout << first << " ";
  out(rest...);
  return;
}
const double EPS = 1e-9;
const int mod = 1e9 + 7;
const int INF = 1e9;
const long long INFLL = 1e18;
void iosetup() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(10);
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << p.first << " " << p.second;
  return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v)
    is >> in;
  return is;
}
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first - t.first, s.second - t.second);
}
template <class S, class T>
pair<S, T> operator*(const pair<S, T> &s, const S &t) {
  return pair<S, T>(s.first * t, s.second * t);
}
template <typename T> void Exit(T first) {
  cout << first << endl;
  exit(0);
};
template <int mod> struct ModInt {
  unsigned x;
  ModInt() : x(0) {}
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  static int get_mod() { return mod; }
};
using modint = ModInt<mod>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const pii dxy[4] = {pii(1, 0), pii(0, 1), pii(-1, 0), pii(0, -1)};
const int calender[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int ucalender[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool range(int a, int b, int x) {
  if (a <= x and x < b)
    return true;
  else
    return false;
}
bool range(int a, int b, int c, int d, pii p) {
  if (a <= p.first and p.first < b and c <= p.second and p.second < d)
    return true;
  else
    return false;
}

template <typename T> struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;
void add_edge(UnWeightedGraph &g, int x, int y) {
  g[x].push_back(y);
  g[y].push_back(x);
}
template <typename T> void add_edge(WeightedGraph<T> &g, int x, int y, T c) {
  g[x].push_back({y, c});
  g[y].push_back({x, c});
}
int S;
template <typename T> vector<vector<ll>> dijkstra(WeightedGraph<T> &g, int s) {
  const auto INF = numeric_limits<T>::max();
  vector<vector<ll>> dp(g.size(), vector<ll>(2510, INFLL));

  using Pi = pair<T, int>;
  priority_queue<Pi, vector<Pi>, greater<Pi>> que;
  if (S >= 2500)
    S = 2500;
  dp[s][S] = 0;
  que.emplace(T(dp[s][S], S), 0);
  ll cnt = 0;
  while (!que.empty()) {
    T cost;
    int idx;
    tie(cost, idx) = que.top();
    // cerr << idx << endl;
    // cerr << cost << endl;
    que.pop();
    if (dp[idx][cost.second] < cost.first)
      continue;
    for (auto &e : g[idx]) {
      auto next_cost = cost + e.cost;
      if (next_cost.second >= 2500)
        next_cost.second = 2500;
      if (dp[e.to][next_cost.second] <= next_cost.first)
        continue;
      if (next_cost.second < 0)
        continue;
      dp[e.to][next_cost.second] = next_cost.first;
      que.emplace(next_cost, e.to);
    }
    cnt++;
  }
  return dp;
}
// dijkstra(g, s);

int main() {
  iosetup();
  int N, M;
  cin >> N >> M >> S;
  WeightedGraph<pll> g(N);
  rep(i, 0, M) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    u--, v--;
    add_edge(g, u, v, pll(b, -a));
  }
  // cerr << "fajdlaf" << endl;
  rep(i, 0, N) {
    ll c, d;
    cin >> c >> d;
    g[i].push_back({i, {d, c}});
  }
  // cerr << "fjsaf" << endl;
  auto dp = dijkstra<pll>(g, 0);
  rep(i, 1, N) {
    ll ans = INFLL;
    rep(j, 0, 2501) chmin(ans, dp[i][j]);
    cout << ans << endl;
  }

  return 0;
}