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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using P = pair<ll, int>;

#define INF 1e18

struct Edge {
  int to;
  ll cost;
  Edge(int to = 0, ll cost = 0) : to(to), cost(cost) {}
};

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

int main() {
  int n;
  cin >> n;
  vector<string> s(n);
  vector<int> c(n);
  rep(i, n) cin >> s[i] >> c[i];
  rep(i, n) {
    s.push_back(rev(s[i]));
    c.push_back(c[i]);
  }
  int idcnt = 1;
  vector<vector<int>> id(n * 2);
  rep(i, n * 2) {
    int l = s[i].size();
    id[i].resize(l);
    rep(j, l) id[i][j] = idcnt++;
  }

  vector<vector<Edge>> G(idcnt);
  rep(i1, n * 2) {
    int l1 = s[i1].size();
    rep(j, l1) {
      int v = id[i1][j];
      rep(i2, n * 2) {
        if (i1 / n == i2 / n)
          continue;
        int l2 = s[i2].size();
        int w = min(l2, l1 - j);
        if (s[i1].substr(j, w) != s[i2].substr(0, w))
          continue;
        int u;
        if (l2 > w)
          u = id[i2][w];
        else if (l1 - j > w)
          u = id[i1][j + w];
        else
          u = 0;
        G[v].emplace_back(u, c[i2]);
      }
    }
  }

  vector<ll> dist(idcnt, INF);
  priority_queue<P, vector<P>, greater<P>> q;
  auto push = [&](int v, ll x) {
    if (dist[v] <= x)
      return;
    dist[v] = x;
    q.emplace(x, v);
  };

  rep(i, n) push(id[i][0], c[i]);
  while (!q.empty()) {
    int v;
    ll c;
    tie(c, v) = q.top();
    q.pop();
    if (c != dist[v])
      continue;
    for (auto e : G[v])
      push(e.to, c + e.cost);
  }

  ll ans = dist[0];
  rep(i, n * 2) {
    int l = s[i].size();
    rep(j, l) {
      string t = s[i].substr(j);
      if (t == rev(t))
        ans = min(ans, dist[id[i][j]]);
    }
  }
  if (ans == INF)
    ans = -1;

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using P = pair<ll, int>;

#define INF 1e18

enum Dir { HEAD, TAIL };

struct Stat {
  string str;
  Dir dir;
  bool start;
  bool end;
  ll cost;
  Stat(string str, Dir dir, bool start, bool end, ll cost)
      : str(str), dir(dir), start(start), end(end), cost(cost) {}
};

struct Edge {
  int to;
  ll cost;
  Edge(int to, ll cost) : to(to), cost(cost) {}
};

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

int main() {
  int n;
  cin >> n;
  vector<string> s;
  vector<int> c;
  map<string, int> mp;
  rep(i, n) {
    string ts;
    int tc;
    cin >> ts >> tc;
    bool found = false;
    rep(j, s.size()) {
      if (ts == s[j]) {
        c[j] = min(c[j], tc);
        mp[s[i]] = c[i];
        found = true;
      }
    }
    if (!found) {
      s.push_back(ts);
      c.push_back(tc);
      mp[ts] = tc;
    }
  }
  rep(i, s.size()) mp[s[i]] = c[i];

  vector<Stat> st;
  st.emplace_back("", HEAD, false, true, INF);
  st.emplace_back("", TAIL, false, true, INF);
  rep(i, s.size()) {
    int l = s[i].size();
    rep(k, l) {
      string s1 = s[i].substr(k, l - k);
      string s2 = s[i].substr(0, k + 1);
      st.emplace_back(s1, HEAD, k == 0, s1 == rev(s1), INF);
      st.emplace_back(s2, TAIL, k == l - 1, s2 == rev(s2), INF);
    }
  }

  int sts = st.size();
  vector<vector<Edge>> G(sts);
  rep(i, sts) {
    if (st[i].end)
      continue;
    rep(j, sts) {
      if (st[i].dir == st[j].dir)
        continue;
      string ss;
      if (st[i].dir == HEAD) {
        ss = st[j].str + rev(st[i].str);
      } else {
        ss = rev(st[i].str) + st[j].str;
      }
      if (mp.count(ss))
        G[i].emplace_back(j, mp[ss]);
    }
  }

  priority_queue<P, vector<P>, greater<P>> q;
  rep(i, sts) if (st[i].start) {
    int c = mp[st[i].str];
    st[i].cost = c;
    q.push({c, i});
  }
  while (!q.empty()) {
    int v;
    ll c;
    tie(c, v) = q.top();
    q.pop();
    if (c != st[v].cost)
      continue;
    for (auto e : G[v]) {
      if (st[e.to].cost <= st[v].cost + e.cost)
        continue;
      st[e.to].cost = st[v].cost + e.cost;
      q.push({st[e.to].cost, e.to});
    }
  }

  ll ans = INF;
  rep(i, sts) if (st[i].end) ans = min(ans, st[i].cost);
  if (ans == INF)
    ans = -1;

  cout << ans << endl;

  return 0;
}