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

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

#define endl '\\n'
#define sz(x) ((int)(x.size()))

string s[55];
long long C[55];

long long dist[55][22][55][22];

bool pal(string s) {
  for (int i = 0; i < sz(s); i++)
    if (s[i] != s[sz(s) - 1 - i])
      return 0;
  return 1;
}

bool pal(string x, string y) {
  for (int i = 0; i < min(sz(x), sz(y)); i++) {
    if (x[i] != y[sz(y) - 1 - i])
      return 0;
  }
  return 1;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> s[i] >> C[i];
  long long ans = 1e18;
  memset(dist, 127, sizeof dist);
  priority_queue<pair<long long, vector<int>>> pq;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j) {
        if (pal(s[i]))
          ans = min(ans, C[i]);
      }
      if (pal(s[i], s[j])) {
        if (sz(s[i]) > sz(s[j])) {
          dist[i][sz(s[i]) - sz(s[j])][j][0] = C[i] + C[j];
          pq.push({-C[i] - C[j], {i, sz(s[i]) - sz(s[j]), j, 0}});
        } else if (sz(s[i]) < sz(s[j])) {
          dist[i][0][j][sz(s[j]) - sz(s[i])] = C[i] + C[j];
          pq.push({-C[i] - C[j], {i, 0, j, sz(s[j]) - sz(s[i])}});
        } else {
          ans = min(ans, C[i] + C[j]);
        }
      }
    }
  }
  while (sz(pq)) {
    auto e = pq.top();
    pq.pop();
    long long D = -e.first;
    auto state = e.second;
    if (state[1] + state[3] == 0) {
      ans = min(ans, D);
      continue;
    }
    int i = state[0];
    int j = state[2];
    if (dist[i][state[1]][j][state[3]] < D)
      continue;
    if (state[3] == 0) {
      if (pal(s[i].substr(sz(s[i]) - state[1]))) {
        ans = min(ans, D);
      } else {
        for (int jj = 0; jj < n; jj++) {
          bool flag = 1;
          for (int k = sz(s[i]) - state[1], t = sz(s[jj]) - 1;
               k < sz(s[i]) && t >= 0; k++, t--) {
            if (s[i][k] != s[jj][t]) {
              flag = 0;
              break;
            }
          }
          if (flag) {
            vector<int> nx;
            if (state[1] >= sz(s[jj])) {
              nx = {i, state[1] - sz(s[jj]), jj, 0};
            } else {
              nx = {i, 0, jj, sz(s[jj]) - state[1]};
            }
            if (D + C[jj] < dist[nx[0]][nx[1]][nx[2]][nx[3]]) {
              dist[nx[0]][nx[1]][nx[2]][nx[3]] = D + C[jj];
              pq.push({-dist[nx[0]][nx[1]][nx[2]][nx[3]], nx});
            }
          }
        }
      }
    } else {
      if (pal(s[j].substr(0, state[3]))) {
        ans = min(ans, D);
      } else {
        for (int ii = 0; ii < n; ii++) {
          bool flag = 1;
          for (int k = state[3] - 1, t = 0; k >= 0 && t < sz(s[ii]); k--, t++) {
            if (s[j][k] != s[ii][t]) {
              flag = 0;
              break;
            }
          }
          if (flag) {
            vector<int> nx;
            if (state[3] >= sz(s[ii])) {
              nx = {ii, 0, j, state[3] - sz(s[ii])};
            } else {
              nx = {ii, sz(s[ii]) - state[3], j, 0};
            }
            if (D + C[ii] < dist[nx[0]][nx[1]][nx[2]][nx[3]]) {
              dist[nx[0]][nx[1]][nx[2]][nx[3]] = D + C[ii];
              pq.push({-dist[nx[0]][nx[1]][nx[2]][nx[3]], nx});
            }
          }
        }
      }
    }
  }
  if (ans > 5e17)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

#define endl '\\n'
#define sz(x) ((int)(x.size()))

string s[55];
long long C[55];

int dist[55][22][55][22];

bool pal(string s) {
  for (int i = 0; i < s.size() / 2; i++)
    if (s[i] != s[s.size() - 1 - i])
      return 0;
  return 1;
}

bool pal(string x, string y) {
  for (int i = 0; i < min(x.size(), y.size()); i++) {
    if (x[i] != y[y.size() - 1 - i])
      return 0;
  }
  return 1;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> s[i] >> C[i];
  long long ans = 1e18;
  memset(dist, 63, sizeof dist);
  priority_queue<pair<long long, vector<int>>> pq;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j) {
        if (pal(s[i]))
          ans = min(ans, C[i]);
      }
      if (pal(s[i], s[j])) {
        if (s[i].size() > s[j].size()) {
          dist[i][sz(s[i]) - sz(s[j])][j][0] = C[i] + C[j];
          pq.push({-C[i] - C[j], {i, sz(s[i]) - sz(s[j]), j, 0}});
        } else if (s[i].size() < s[j].size()) {
          dist[i][0][j][sz(s[j]) - sz(s[i])] = C[i] + C[j];
          pq.push({-C[i] - C[j], {i, 0, j, sz(s[j]) - sz(s[i])}});
        } else {
          ans = min(ans, C[i] + C[j]);
        }
      }
    }
  }
  while (pq.size()) {
    auto e = pq.top();
    pq.pop();
    long long D = -e.first;
    auto state = e.second;
    if (state[1] + state[3] == 0) {
      ans = min(ans, D);
      continue;
    }
    if (dist[state[0]][state[1]][state[2]][state[3]] < D)
      continue;
    int i = state[0];
    int j = state[2];
    if (state[3] == 0) {
      if (pal(s[i].substr(sz(s[j])))) {
        ans = min(ans, D);
      } else {
        for (int jj = 0; jj < n; jj++) {
          bool flag = 1;
          for (int k = sz(s[j]), t = sz(s[jj]) - 1; k < sz(s[i]) && t >= 0;
               k++, t--) {
            if (s[i][k] != s[jj][t]) {
              flag = 0;
              break;
            }
          }
          if (flag) {
            vector<int> nx;
            if (state[1] >= sz(s[jj])) {
              nx = {state[0], state[1] - sz(s[jj]), jj, 0};
            } else {
              nx = {state[0], 0, jj, sz(s[jj]) - state[1]};
            }
            if (D + C[jj] < dist[nx[0]][nx[1]][nx[2]][nx[3]]) {
              dist[nx[0]][nx[1]][nx[2]][nx[3]] = D + C[jj];
              pq.push({-dist[nx[0]][nx[1]][nx[2]][nx[3]], nx});
            }
          }
        }
      }
    } else {
      if (pal(s[j].substr(0, state[3]))) {
        ans = min(ans, D);
      } else {
        for (int ii = 0; ii < n; ii++) {
          bool flag = 1;
          for (int k = state[3] - 1, t = 0; k >= 0 && t < sz(s[ii]); k--, t++) {
            if (s[j][k] != s[ii][t]) {
              flag = 0;
              break;
            }
          }
          if (flag) {
            vector<int> nx;
            if (state[3] >= sz(s[ii])) {
              nx = {ii, 0, j, state[3] - sz(s[ii])};
            } else {
              nx = {ii, sz(s[ii]) - state[3], j, 0};
            }
            if (D + C[ii] < dist[nx[0]][nx[1]][nx[2]][nx[3]]) {
              dist[nx[0]][nx[1]][nx[2]][nx[3]] = D + C[ii];
              pq.push({-dist[nx[0]][nx[1]][nx[2]][nx[3]], nx});
            }
          }
        }
      }
    }
  }
  if (ans > 1e17)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}