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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
#define yn                                                                     \\
  { puts("Yes"); }                                                             \\
  else {                                                                       \\
    puts("No");                                                                \\
  }
struct edge {
  ll to, cost;
};
const ll INF = 1001001001001001001;
const int MAX_N = 200005;
const int MAX_V = 200005;
vector<edge> G[MAX_V];
ll d[MAX_V];

void dijkstra(int s) {
  // greater<P>を指定することでfirstが小さい順に取り出せるようにする
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + MAX_V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int n;
  cin >> n;
  string s[n][2];
  ll c[n];
  rep(i, n) cin >> s[i][0] >> c[i];
  rep(i, n) {
    s[i][1] = s[i][0];
    reverse(s[i][1].begin(), s[i][1].end());
  }
  int m[n];
  set<string> se[2];
  rep(i, n) {
    m[i] = s[i][0].size();
    rep(j, 2) {
      string tmp;
      rep(k, m[i]) {
        tmp = s[i][j].substr(k);
        se[j].insert(tmp);
      }
    }
  }

  map<string, int> mp[2];
  int cnt = 1;
  rep(j, 2) {
    for (auto x : se[j]) {
      mp[j][x] = cnt;
      cnt++;
    }
  }

  rep(j, 2) {
    rep(i, n) {
      edge e;
      e.to = mp[j][s[i][j]];
      e.cost = c[i];
      G[0].push_back(e);
    }
  }

  rep(k, 2) {
    for (auto p : mp[k]) {
      int id = p.second;
      string ss = p.first;
      int mm = ss.size();
      rep(i, n) {
        if (m[i] == mm) {
          if (ss == s[i][(k + 1) % 2]) {
            edge e;
            e.to = cnt;
            e.cost = c[i];
            G[id].push_back(e);
          }
        } else if (m[i] < mm) {
          int ng = 0;
          rep(j, m[i]) {
            if (ss[j] != s[i][(k + 1) % 2][j]) {
              ng = 1;
              break;
            }
          }
          if (ng == 0) {
            string tmp = ss.substr(m[i]);
            int id2 = mp[k][tmp];
            edge e;
            e.to = id2;
            e.cost = c[i];
            G[id].push_back(e);
          }
        } else {
          int ng = 0;
          rep(j, mm) {
            if (ss[j] != s[i][(k + 1) % 2][j]) {
              ng = 1;
              break;
            }
          }
          if (ng == 0) {
            string tmp = s[i][(k + 1) % 2].substr(mm);
            int id2 = mp[(k + 1) % 2][tmp];
            edge e;
            e.to = id2;
            e.cost = c[i];
            G[id].push_back(e);
          }
        }
      }
    }
  }

  dijkstra(0);

  ll ans = d[cnt];
  rep(i, n) {
    if (s[i][0] == s[i][1]) {
      ans = min(ans, c[i]);
    }
  }

  rep(j, 2) {
    for (auto p : mp[j]) {
      string ss = p.first;
      string tt = ss;
      reverse(tt.begin(), tt.end());
      if (ss == tt) {
        int id = p.second;
        ans = min(ans, d[id]);
      }
    }
  }

  if (ans >= INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
#define yn                                                                     \\
  { puts("Yes"); }                                                             \\
  else {                                                                       \\
    puts("No");                                                                \\
  }
struct edge {
  ll to, cost;
};
const ll INF = 1001001001001001001;
const int MAX_N = 200005;
const int MAX_V = 200005;
vector<edge> G[MAX_V];
ll d[MAX_V];

void dijkstra(int s) {
  // greater<P>を指定することでfirstが小さい順に取り出せるようにする
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + MAX_V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int n;
  cin >> n;
  string s[n];
  ll c[n];
  rep(i, n) cin >> s[i] >> c[i];
  string t[n];
  rep(i, n) {
    t[i] = s[i];
    reverse(t[i].begin(), t[i].end());
  }
  int m[n];
  set<string> se1, se2;
  rep(i, n) {
    m[i] = s[i].size();
    string tmp;
    rep(j, m[i]) {
      tmp += s[j];
      se1.insert(tmp);
    }
    tmp.clear();
    rep(j, m[i]) {
      tmp += t[j];
      se2.insert(tmp);
    }
  }
  map<string, int> mp1, mp2;
  int cnt = 1;
  for (auto x : se1) {
    mp1[x] = cnt;
    cnt++;
  }
  for (auto x : se2) {
    mp2[x] = cnt;
    cnt++;
  }

  rep(i, n) {
    edge e;
    e.to = mp1[s[i]];
    e.cost = c[i];
    G[0].push_back(e);
  }

  for (auto p : mp1) {
    int id = p.second;
    string ss = p.first;
    int mm = ss.size();
    rep(i, n) {
      if (m[i] == mm) {
        if (ss == t[i]) {
          edge e;
          e.to = cnt;
          e.cost = c[i];
          G[id].push_back(e);
        }
      } else if (m[i] < mm) {
        int ng = 0;
        rep(j, m[i]) {
          if (ss[j] != t[i][j]) {
            ng = 1;
            break;
          }
        }
        if (ng == 0) {
          string tmp = ss.substr(m[i]);
          int id2 = mp1[tmp];
          edge e;
          e.to = id2;
          e.cost = c[i];
          G[id].push_back(e);
        }
      } else {
        int ng = 0;
        rep(j, mm) {
          if (ss[j] != t[i][j]) {
            ng = 1;
            break;
          }
        }
        if (ng == 0) {
          string tmp = t[i].substr(mm);
          int id2 = mp2[tmp];
          edge e;
          e.to = id2;
          e.cost = c[i];
          G[id].push_back(e);
        }
      }
    }
  }

  for (auto p : mp2) {
    int id = p.second;
    string ss = p.first;
    int mm = ss.size();
    rep(i, n) {
      if (m[i] == mm) {
        if (ss == s[i]) {
          edge e;
          e.to = cnt;
          e.cost = c[i];
          G[id].push_back(e);
        }
      } else if (m[i] < mm) {
        int ng = 0;
        rep(j, m[i]) {
          if (ss[j] != s[i][j]) {
            ng = 1;
            break;
          }
        }
        if (ng == 0) {
          string tmp = ss.substr(m[i]);
          int id2 = mp2[tmp];
          edge e;
          e.to = id2;
          e.cost = c[i];
          G[id].push_back(e);
        }
      } else {
        int ng = 0;
        rep(j, mm) {
          if (ss[j] != s[i][j]) {
            ng = 1;
            break;
          }
        }
        if (ng == 0) {
          string tmp = s[i].substr(mm);
          int id2 = mp1[tmp];
          edge e;
          e.to = id2;
          e.cost = c[i];
          G[id].push_back(e);
        }
      }
    }
  }

  dijkstra(0);
  if (d[cnt] >= INF)
    d[cnt] = -1;
  cout << d[cnt] << endl;
  return 0;
}