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

#include <bits/stdc++.h>
using Int = long long; // clang-format off
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(v) std::begin(v), std::end(v)
#define RALL(v) std::rbegin(v), std::rend(v)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif // clang-format on

/**
 *    author:  knshnb
 *    created: Sat Aug 15 22:15:43 JST 2020
 **/

template <class T> bool chmin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}
template <class T> bool chmax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <class T, class S> T make_vec(S x) { return x; }
template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) {
  return std::vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...));
}

template <class Dist, class Key,
          class Delta> // Delta: Key, (Key, Dist -> void) -> void
std::map<Key, Dist> dijkstra(Key s, Delta delta) {
  std::map<Key, Dist> dist;
  using P = std::pair<Dist, Key>;
  std::priority_queue<P, std::vector<P>, std::greater<P>> q;
  q.push({dist[s] = Dist(), s});
  while (!q.empty()) {
    std::pair<Dist, Key> p = q.top();
    q.pop();
    if (dist[p.second] < p.first)
      continue;
    delta(p.second, [&](Key to, Dist d) -> void {
      if (dist.count(to) && dist[to] <= p.first + d)
        return;
      q.push({dist[to] = p.first + d, to});
    });
  }
  return dist;
}

bool is_palindrome(const std::string &s) {
  REP(i, s.size()) {
    if (s[i] != s[s.size() - 1 - i])
      return false;
  }
  return true;
}
const Int INF = 1e18;
const std::string dummy = "01";
signed main() {
  Int n;
  std::cin >> n;
  auto ss = make_vec<std::string>(2, n, "");
  std::vector<Int> c(n);
  REP(i, n)
      std::cin >> ss[0][i] >> c[i],
      ss[1][i] = std::string(RALL(ss[0][i]));
  using Key = std::pair<std::string, bool>;
  auto delta = [&](Key from, auto update) -> void {
    REP(i, n) {
      std::string s = from.first == dummy ? "" : from.first;
      std::string t = ss[!from.second][i];
      bool nxt_b = from.second ^ (s.size() < t.size());
      if (s > t)
        std::swap(s, t);
      if (s != t.substr(0, s.size()))
        continue;
      std::string nxt_s = t.substr(s.size(), t.size() - s.size());
      update(Key{nxt_s, nxt_b}, c[i]);
    }
  };
  Key s = {dummy, 0};
  auto res = dijkstra<Int>(s, delta);
  Int ans = INF;
  for (auto &p : res) {
    if (is_palindrome(p.first.first))
      chmin(ans, p.second);
  }
  std::cout << (ans == INF ? -1 : ans) << std::endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using Int = long long; // clang-format off
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(v) std::begin(v), std::end(v)
#define RALL(v) std::rbegin(v), std::rend(v)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif // clang-format on

/**
 *    author:  knshnb
 *    created: Sat Aug 15 22:15:43 JST 2020
 **/

template <class T> bool chmin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}
template <class T> bool chmax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <class T, class S> T make_vec(S x) { return x; }
template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) {
  return std::vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...));
}

bool is_palindrome(const std::string &s) {
  REP(i, s.size()) {
    if (s[i] != s[s.size() - 1 - i])
      return false;
  }
  return true;
}
const Int INF = 1e18;
signed main() {
  Int n;
  std::cin >> n;
  auto ss = make_vec<std::string>(2, n, "");
  std::vector<Int> c(n);
  REP(i, n)
      std::cin >> ss[0][i] >> c[i],
      ss[1][i] = std::string(RALL(ss[0][i]));
  using Key = std::pair<std::string, bool>;
  std::map<Key, Int> mp;
  auto dfs = [&](auto &&self, Key key) -> Int {
    if (mp.count(key))
      return mp[key];
    if (is_palindrome(key.first))
      return mp[key] = 0;
    Int ans = INF;
    REP(i, n) {
      std::string s = key.first, t = ss[!key.second][i];
      bool nxt_b = key.second ^ (s.size() < t.size());
      if (s > t)
        std::swap(s, t);
      if (s != t.substr(0, s.size()))
        continue;
      std::string nxt_s = t.substr(s.size(), t.size() - s.size());
      Int score =
          (is_palindrome(nxt_s) ? 0 : self(self, {nxt_s, nxt_b})) + c[i];
      chmin(ans, score);
    }
    return mp[key] = ans;
  };
  Int ans = INF;
  REP(i, n) {
    Int score = dfs(dfs, {ss[0][i], 0}) + c[i];
    chmin(ans, score);
  }
  std::cout << (ans == INF ? -1 : ans) << std::endl;
}