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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

struct D {
  int cost;
  vector<int> e;
  D() : cost(INT_MAX), e(26, -1) {}
};

void addtr(vector<D> &tr, int tri[50][20], int si, string s, int c, bool rev) {
  int cur = 0;
  rep(j, s.size()) {
    int sj = rev ? (s.size() - 1 - j) : j;
    int ci = s[sj] - 'a';
    int nxt = tr[cur].e[ci];
    if (nxt == -1) {
      tr.push_back(D());
      nxt = tr[cur].e[ci] = tr.size() - 1;
    }
    tri[si][sj] = nxt;
    cur = nxt;
  }
  tr[cur].cost = min(tr[cur].cost, c);
}

ll mm[1000][1000];
vector<int> ssame[1000][1000];

int main() {
  int n;
  cin >> n;
  vector<string> s(n);
  vector<int> c(n);
  rep(i, n) cin >> s[i] >> c[i];

  vector<D> tr(1), rtr(1);
  int tri[50][20], rtri[50][20];
  rep(i, n) addtr(tr, tri, i, s[i], c[i], 0);
  rep(i, n) addtr(rtr, rtri, i, s[i], c[i], 1);

  rep(i, n) rep(j, s[i].size()) {
    int ti = tri[i][j], rti = rtri[i][j];
    ssame[ti][rti].push_back(i);
  }

  rep(i, 1000) rep(j, 1000) mm[i][j] = LLONG_MAX;
#define PP pair<long long, pair<int, int>>
  priority_queue<PP, vector<PP>, greater<PP>> pq;
  pq.push(make_pair(LLONG_MAX, make_pair(0, 0)));
  bool first = true;
  ll mn = LLONG_MAX;
  while (!pq.empty()) {
    auto p = pq.top();
    pq.pop();
    ll cc = p.first;
    int a = p.second.first, b = p.second.second;
    if (cc > mm[a][b])
      continue;

    if (!first) {
      for (int si : ssame[a][b])
        mn = min(mn, mm[a][b] + c[si]);
      rep(k, 26) {
        int na = tr[a].e[k];
        if (na != -1)
          for (int si : ssame[na][b])
            mn = min(mn, mm[a][b] + c[si]);
      }
      if (tr[a].cost != INT_MAX && rtr[b].cost != INT_MAX)
        mn = min(mn, mm[a][b] + tr[a].cost + rtr[b].cost);
    }

    rep(l, 4) {
      int ca = a, cb = b;
      ll cm = mm[a][b];
      if (l % 2 == 1) {
        if (tr[a].cost == INT_MAX)
          continue;
        ca = 0;
        cm += tr[a].cost;
      }
      if (l / 2 == 1) {
        if (rtr[b].cost == INT_MAX)
          continue;
        cb = 0;
        cm += rtr[b].cost;
      }
      if (first) {
        first = false;
        cm = 0;
      }
      rep(k, 26) {
        int na = tr[ca].e[k], nb = rtr[cb].e[k];
        if (na != -1 && nb != -1)
          if (cm < mm[na][nb]) {
            mm[na][nb] = cm;
            pq.push(make_pair(cm, make_pair(na, nb)));
          }
      }
    }
  }

  if (mn == LLONG_MAX)
    cout << (-1) << endl;
  else
    cout << mn << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

struct D {
  char cha;
  int cost;
  vector<int> e;
  D(char cha = 0, int cost = INT_MAX) : cha(cha), cost(cost), e(26, -1) {}
};

void addtr(vector<D> &tr, int tri[50][20], int si, string s, int c, bool rev) {
  int cur = 0;
  rep(j, s.size()) {
    int sj = rev ? (s.size() - 1 - j) : j;
    int cha = s[j];
    if (rev)
      cha = s[sj];
    int ci = cha - 'a';
    int nxt = tr[cur].e[ci];
    if (nxt == -1) {
      tr.push_back(D(cha));
      nxt = tr[cur].e[ci] = tr.size() - 1;
      tri[si][sj] = nxt;
    }
    cur = nxt;
  }
  tr[cur].cost = min(tr[cur].cost, c);
}

ll mm[1000][1000];
vector<int> ssame[1000][1000];

int main() {
  int n;
  cin >> n;
  vector<string> s(n);
  vector<int> c(n);
  rep(i, n) cin >> s[i] >> c[i];

  vector<D> tr(1, D(0, 0)), rtr(1, D(0, 0));
  int tri[50][20], rtri[50][20];
  rep(i, n) addtr(tr, tri, i, s[i], c[i], 0);
  rep(i, n) addtr(rtr, rtri, i, s[i], c[i], 1);

  rep(i, n) rep(j, s[i].size()) {
    int ti = tri[i][j], rti = rtri[i][j];
    ssame[ti][rti].push_back(i);
  }

  rep(i, 1000) rep(j, 1000) mm[i][j] = LLONG_MAX;
#define PP pair<long long, pair<int, int>>
  priority_queue<PP, vector<PP>, greater<PP>> q;
  q.push(make_pair(LLONG_MAX, make_pair(0, 0)));
  bool first = true;
  ll mn = LLONG_MAX;
  while (!q.empty()) {
    auto p = q.top();
    q.pop();
    ll cc = p.first;
    int a = p.second.first, b = p.second.second;
    if (cc > mm[a][b])
      continue;

    if (!first) {
      for (int si : ssame[a][b])
        mn = min(mn, mm[a][b] + c[si]);
      rep(k, 26) {
        int na = tr[a].e[k];
        if (na != -1)
          for (int si : ssame[na][b])
            mn = min(mn, mm[a][b] + c[si]);
      }
      if (tr[a].cost != INT_MAX && rtr[b].cost != INT_MAX)
        mn = min(mn, mm[a][b] + tr[a].cost + rtr[b].cost);
    }

    rep(l, 4) {
      int ca = a, cb = b;
      ll cm = mm[a][b];
      if (l % 2 == 1) {
        if (tr[a].cost == INT_MAX)
          continue;
        ca = 0;
        cm += tr[a].cost;
      }
      if (l / 2 == 1) {
        if (rtr[b].cost == INT_MAX)
          continue;
        cb = 0;
        cm += rtr[b].cost;
      }
      if (first) {
        first = false;
        cm = 0;
      }
      rep(k, 26) {
        int na = tr[ca].e[k], nb = rtr[cb].e[k];
        if (na != -1 && nb != -1)
          if (cm < mm[na][nb]) {
            mm[na][nb] = cm;
            q.push(make_pair(cm, make_pair(na, nb)));
          }
      }
    }
  }

  if (mn == LLONG_MAX)
    cout << (-1) << endl;
  else
    cout << mn << endl;
  return 0;
}