Problem p02587 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int h(int tp, int id, int len) { return (tp + (id << 1) + (len << 7)); }

string rev(string s) {
  reverse(s.begin(), s.end());
  return s;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  const int N = (1 << 12);
  const ll inf = (1ll << 60);
  vector<array<ll, 2>> v[N];
  int n;
  cin >> n;
  vector<string> s(n + 1);
  vector<ll> c(n + 1, 0);
  for (int i = 1; i <= n; i++)
    cin >> s[i] >> c[i];
  for (int i = 1; i <= n; i++) {
    int one = s[i].size();
    for (int len = 1; len <= one; len++) {
      for (int j = 1; j <= n; j++) {
        int sz = s[j].size();
        // prefix-0
        if (sz < len && s[i].substr(len - sz, sz) == rev(s[j]))
          v[h(0, i, len)].push_back({h(0, i, len - sz), c[j]});
        if (sz == len && s[i].substr(0, len) == rev(s[j]))
          v[h(0, i, len)].push_back({0, c[j]});
        if (sz > len && s[i].substr(0, len) == rev(s[j].substr(0, len)))
          v[h(0, i, len)].push_back({h(1, j, sz - len), c[j]});
        // suffix-1
        if (sz < len && s[i].substr(one - len, sz) == rev(s[j]))
          v[h(1, i, len)].push_back({h(1, i, len - sz), c[j]});
        if (sz == len && s[i].substr(one - len, len) == rev(s[j]))
          v[h(1, i, len)].push_back({0, c[j]});
        if (sz > len &&
            s[i].substr(one - len, len) == rev(s[j].substr(sz - len, len)))
          v[h(1, i, len)].push_back({h(0, j, sz - len), c[j]});
      }
    }
  }
  priority_queue<array<ll, 2>, vector<array<ll, 2>>, greater<array<ll, 2>>> q;
  vector<ll> d(N, inf);
  auto mn = [](ll &a, ll b) { a = min(a, b); };
  for (int i = 1; i <= n; i++) {
    int sz = s[i].size();
    if (s[i] == rev(s[i]))
      mn(d[0], c[i]);
    // one
    for (int j = 0; j < sz - j - 1; j++)
      if (s[i].substr(0, j) == rev(s[i].substr(j + 1, j)))
        mn(d[h(1, i, sz - (2 * j + 1))], c[i]);
    for (int j = sz - 1; j > sz - j - 1; j--)
      if (s[i].substr(j + 1, sz - j - 1) ==
          rev(s[i].substr(j - (sz - j - 1), sz - j - 1)))
        mn(d[h(0, i, sz - (2 * (sz - j - 1) + 1))], c[i]);
    // two
    for (int j = 0; j < sz - j - 2; j++)
      if (s[i].substr(0, j + 1) == rev(s[i].substr(j + 1, j + 1)))
        mn(d[h(1, i, sz - (2 * j + 2))], c[i]);
    for (int j = sz - 2; j > sz - j - 2; j--)
      if (s[i].substr(j + 1, sz - j - 1) ==
          rev(s[i].substr(j - (sz - j - 1), sz - j - 1)))
        mn(d[h(0, i, sz - (2 * j + 2))], c[i]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      int one = s[i].size();
      int two = s[j].size();
      if (one == two && s[i] + s[j] == rev(s[i] + s[j]))
        mn(d[0], c[i] + c[j]);
      if (one < two && s[i] == rev(s[j].substr(0, one)))
        mn(d[h(1, j, two - one)], c[i] + c[j]);
      if (one > two && s[i].substr(one - two, two) == rev(s[j]))
        mn(d[h(0, i, one - two)], c[i] + c[j]);
    }
  }
  for (int i = 0; i < N; i++)
    if (d[i] < inf)
      q.push({d[i], i});
  while (!q.empty()) {
    auto [da, a] = q.top();
    q.pop();
    if (d[a] < da)
      continue;
    for (auto [to, w] : v[a]) {
      if (d[a] + w < d[to]) {
        d[to] = d[a] + w;
        q.push({d[to], to});
      }
    }
  }
  if (d[0] == inf)
    d[0] = -1;
  cout << d[0] << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int h(int tp, int id, int len) { return (tp + (id << 1) + (len << 7)); }

string rev(string s) {
  reverse(s.begin(), s.end());
  return s;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  const int N = (1 << 12);
  const ll inf = (1ll << 60);
  vector<array<ll, 2>> v[N];
  int n;
  cin >> n;
  vector<string> s(n + 1);
  vector<ll> c(n + 1, 0);
  for (int i = 1; i <= n; i++)
    cin >> s[i] >> c[i];
  for (int i = 1; i <= n; i++) {
    int one = s[i].size();
    for (int len = 1; len <= one; len++) {
      for (int j = 1; j <= n; j++) {
        int sz = s[j].size();
        // prefix-0
        if (sz < len && s[i].substr(len - sz, sz) == rev(s[j]))
          v[h(0, i, len)].push_back({h(0, i, len - sz), c[j]});
        if (sz == len && s[i].substr(0, len) == rev(s[j]))
          v[h(0, i, len)].push_back({0, c[j]});
        if (sz > len && s[i].substr(0, len) == rev(s[j].substr(0, len)))
          v[h(0, i, len)].push_back({h(1, j, sz - len), c[j]});
        // suffix-1
        if (sz < len && s[i].substr(one - len, sz) == rev(s[j]))
          v[h(1, i, len)].push_back({h(1, i, len - sz), c[j]});
        if (sz == len && s[i].substr(one - len, len) == rev(s[j]))
          v[h(1, i, len)].push_back({0, c[j]});
        if (sz > len &&
            s[i].substr(one - len, len) == rev(s[j].substr(sz - len, len)))
          v[h(1, i, len)].push_back({h(0, j, sz - len), c[j]});
      }
    }
  }
  priority_queue<array<ll, 2>, vector<array<ll, 2>>, greater<array<ll, 2>>> q;
  vector<ll> d(N, inf);
  auto mn = [](ll &a, ll b) { a = min(a, b); };
  for (int i = 1; i <= n; i++) {
    int sz = s[i].size();
    if (s[i] == rev(s[i]))
      mn(d[0], c[i]);
    // one
    for (int j = 0; j < sz - j - 1; j++)
      if (s[i].substr(0, j) == rev(s[i].substr(j + 1, j)))
        mn(d[h(1, i, sz - (2 * j + 1))], c[i]);
    for (int j = sz - 1; j > sz - j - 1; j--)
      if (s[i].substr(j + 1, sz - j - 1) ==
          rev(s[i].substr(j - (sz - j - 1), sz - j - 1)))
        mn(d[h(0, i, sz - (2 * (sz - j - 1) + 1))], c[i]);
    // two
    for (int j = 0; j < sz - j - 2; j++)
      if (s[i].substr(0, j + 1) == rev(s[i].substr(j + 1, j + 1)))
        mn(d[h(1, i, sz - (2 * j + 2))], c[i]);
    for (int j = sz - 2; j > sz - j - 2; j--)
      if (s[i].substr(j + 1, sz - j - 1) ==
          rev(s[i].substr(j - (sz - j - 1) + 1, sz - j - 1)))
        mn(d[h(0, i, sz - (2 * (sz - j - 2) + 2))], c[i]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      int one = s[i].size();
      int two = s[j].size();
      if (one == two && s[i] + s[j] == rev(s[i] + s[j]))
        mn(d[0], c[i] + c[j]);
      if (one < two && s[i] == rev(s[j].substr(0, one)))
        mn(d[h(1, j, two - one)], c[i] + c[j]);
      if (one > two && s[i].substr(one - two, two) == rev(s[j]))
        mn(d[h(0, i, one - two)], c[i] + c[j]);
    }
  }
  for (int i = 0; i < N; i++)
    if (d[i] < inf)
      q.push({d[i], i});
  while (!q.empty()) {
    auto [da, a] = q.top();
    q.pop();
    if (d[a] < da)
      continue;
    for (auto [to, w] : v[a]) {
      if (d[a] + w < d[to]) {
        d[to] = d[a] + w;
        q.push({d[to], to});
      }
    }
  }
  if (d[0] == inf)
    d[0] = -1;
  cout << d[0] << "\n";
  return 0;
}