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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using V = vector<int>;
using Vll = vector<ll>;
using Vld = vector<ld>;
using Vbo = vector<bool>;
using VV = vector<V>;
using VVll = vector<Vll>;
using VVld = vector<Vld>;
using VVbo = vector<Vbo>;
using VVV = vector<VV>;
using VVVll = vector<VVll>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using Pld = pair<ld, ld>;
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
#define all(a) (a).begin(), (a).end()
struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
template <typename T> inline int sz(T &x) { return x.size(); }
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << '(' << p.first << ", " << p.second << ')';
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto &e : v)
    os << e << ' ';
  return os;
}
template <typename T> inline int count_between(vector<T> &a, T l, T r) {
  return lower_bound(all(a), r) - lower_bound(all(a), l);
} // [l, r)
inline int fLog2(const int x) {
  assert(x > 0);
  return 31 - __builtin_clz(x);
} // floor(log2(x))
inline int fLog2(const ll x) {
  assert(x > 0);
  return 63 - __builtin_clzll(x);
}
inline int cLog2(const int x) {
  assert(x > 0);
  return (x == 1) ? 0 : 32 - __builtin_clz(x - 1);
} // ceil(log2(x))
inline int cLog2(const ll x) {
  assert(x > 0);
  return (x == 1) ? 0 : 64 - __builtin_clzll(x - 1);
}
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
void fail() {
  cout << -1 << '\\n';
  exit(0);
}
// const int INF  = 1<<30;
const ll INFll = 1ll << 62;
// const ld EPS   = 1e-10;
// const ld PI    = acos(-1.0);
// const int MOD  = int(1e9)+7;
// const int MOD  = 998244353;

template <typename T> struct edge {
  int v;
  T w;
  edge(int v, T w) : v(v), w(w) {}
};
template <typename T> using Graph = vector<vector<edge<T>>>;

string rev(string s) {
  reverse(all(s));
  return s;
}

map<string, int> idS[2];
vector<string> s[2];

int main() {
  int n;
  cin >> n;
  V c(n);
  rep(i, n) {
    string x;
    cin >> x >> c[i];
    s[0].push_back(x);
    s[1].push_back(rev(x));
  }

  const int M = 21;
  auto nid = [&](int i, int j, int r) { return M * i + j + n * M * r; };

  int nv = n * M * 2;
  Graph<ll> G(nv);

  rep(r, 2) rep(i, n) rep2(j, 1, sz(s[r][i]) + 1) rep(k, n) {
    auto t1 = rev(s[r][i].substr(0, j));
    auto t2 = s[r][k];

    int l = sz(t2);
    int mn = min(j, l);
    if (t1.substr(0, mn) != t2.substr(0, mn))
      continue;

    int r2 = r;
    int i2 = i;
    int j2 = abs(j - l);
    if (j < l)
      r2 ^= 1, i2 = k;

    G[nid(i, j, r)].emplace_back(nid(i2, j2, r2), c[k]);
  }

  using T = ll;
  const auto INF_T = numeric_limits<T>::max();
  vector<T> dist(nv, INF_T);
  using PTi = pair<T, int>;
  priority_queue<PTi, vector<PTi>, greater<PTi>> pq;

  rep(i, n) {
    int x = nid(i, sz(s[0][i]), 0);
    dist[x] = c[i];
    pq.emplace(dist[x], x);
  }

  while (!pq.empty()) {
    auto [d, u] = pq.top();
    pq.pop();
    if (dist[u] < d)
      continue;
    for (auto &[v, w] : G[u]) {
      if (chmin(dist[v], d + w)) {
        pq.emplace(dist[v], v);
      }
    }
  }

  ll ans = INFll;
  rep(r, 2) rep(i, n) rep(j, sz(s[r][i]) + 1) {
    auto t = s[r][i].substr(0, j);
    if (t == rev(t)) {
      chmin(ans, dist[nid(i, j, r)]);
    }
  }

  cout << (ans == INFll ? -1 : ans) << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using V = vector<int>;
using Vll = vector<ll>;
using Vld = vector<ld>;
using Vbo = vector<bool>;
using VV = vector<V>;
using VVll = vector<Vll>;
using VVld = vector<Vld>;
using VVbo = vector<Vbo>;
using VVV = vector<VV>;
using VVVll = vector<VVll>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using Pld = pair<ld, ld>;
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
#define all(a) (a).begin(), (a).end()
struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
template <typename T> inline int sz(T &x) { return x.size(); }
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << '(' << p.first << ", " << p.second << ')';
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto &e : v)
    os << e << ' ';
  return os;
}
template <typename T> inline int count_between(vector<T> &a, T l, T r) {
  return lower_bound(all(a), r) - lower_bound(all(a), l);
} // [l, r)
inline int fLog2(const int x) {
  assert(x > 0);
  return 31 - __builtin_clz(x);
} // floor(log2(x))
inline int fLog2(const ll x) {
  assert(x > 0);
  return 63 - __builtin_clzll(x);
}
inline int cLog2(const int x) {
  assert(x > 0);
  return (x == 1) ? 0 : 32 - __builtin_clz(x - 1);
} // ceil(log2(x))
inline int cLog2(const ll x) {
  assert(x > 0);
  return (x == 1) ? 0 : 64 - __builtin_clzll(x - 1);
}
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
void fail() {
  cout << -1 << '\\n';
  exit(0);
}
// const int INF  = 1<<30;
const ll INFll = 1ll << 62;
// const ld EPS   = 1e-10;
// const ld PI    = acos(-1.0);
// const int MOD  = int(1e9)+7;
// const int MOD  = 998244353;

template <typename T> struct edge {
  int v;
  T w;
  edge(int v, T w) : v(v), w(w) {}
};
template <typename T> using Graph = vector<vector<edge<T>>>;

string rev(string s) {
  reverse(all(s));
  return s;
}

map<string, int> idS[2];
vector<string> s[2];

int main() {
  int n;
  cin >> n;
  V c(n);
  rep(i, n) {
    string x;
    cin >> x >> c[i];
    s[0].push_back(x);
    s[1].push_back(rev(x));
  }

  const int M = 6;
  auto nid = [&](int i, int j, int r) { return M * i + j + n * M * r; };

  int nv = n * M * 2;
  Graph<ll> G(nv);

  rep(r, 2) rep(i, n) rep2(j, 1, sz(s[r][i]) + 1) rep(k, n) {
    auto t1 = rev(s[r][i].substr(0, j));
    auto t2 = s[r][k];

    int l = sz(t2);
    int mn = min(j, l);
    if (t1.substr(0, mn) != t2.substr(0, mn))
      continue;

    int r2 = r;
    int i2 = i;
    int j2 = abs(j - l);
    if (j < l)
      r2 ^= 1, i2 = k;

    G[nid(i, j, r)].emplace_back(nid(i2, j2, r2), c[k]);
  }

  using T = ll;
  const auto INF_T = numeric_limits<T>::max();
  vector<T> dist(nv, INF_T);
  using PTi = pair<T, int>;
  priority_queue<PTi, vector<PTi>, greater<PTi>> pq;

  rep(i, n) {
    int x = nid(i, sz(s[0][i]), 0);
    dist[x] = c[i];
    pq.emplace(dist[x], x);
  }

  while (!pq.empty()) {
    auto [d, u] = pq.top();
    pq.pop();
    if (dist[u] < d)
      continue;
    for (auto &[v, w] : G[u]) {
      if (chmin(dist[v], d + w)) {
        pq.emplace(dist[v], v);
      }
    }
  }

  ll ans = INFll;
  rep(r, 2) rep(i, n) rep(j, sz(s[r][i]) + 1) {
    auto t = s[r][i].substr(0, j);
    if (t == rev(t)) {
      chmin(ans, dist[nid(i, j, r)]);
    }
  }

  cout << (ans == INFll ? -1 : ans) << '\\n';
  return 0;
}