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

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

const long long int ten[10] = {
    1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};

long long int coder(vector<int> &vec) {
  long long int res = 0;
  for (int i = 0; i < vec.size(); i++) {
    res *= 10;
    res += vec[i];
  }
  return res;
}

long long int get(long long int val, int level) {
  return (val / ten[level]) % 10;
}

void swapp(long long int &val, int lhs, int rhs) {
  int lval = get(val, lhs);
  int rval = get(val, rhs);
  val = val - (lval * ten[lhs] + rval * ten[rhs]) +
        (lval * ten[rhs] + rval * ten[lhs]);
  return;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  vector<int> object(n);
  iota(object.begin(), object.end(), 0);
  long long int ob = coder(object);
  map<long long int, int> dp[2];
  for (int loop = 0; loop < 2; loop++) {
    long long int first;
    if (loop == 0)
      first = coder(a);
    else
      first = ob;
    dp[loop][first] = 0;
    queue<pair<long long int, int>> q;
    q.push(make_pair(first, 0));
    while (!q.empty()) {
      auto state = q.front();
      q.pop();
      if (state.second >= 4)
        continue;

      // odd
      for (int i = 0; i < n; i++) {
        auto b = state.first;
        for (int j = 0; j < n; j++) {
          if (!(0 <= i - j && i + j < n))
            break;
          swapp(b, i - j, i + j);
          if (!dp[loop].count(b)) {
            dp[loop][b] = state.second + 1;
            q.push(make_pair(b, state.second + 1));
          }
        }
      }

      // else
      for (int i = 0; i < n - 1; i++) {
        auto b = state.first;
        for (int j = 0; j < n; j++) {
          if (!(0 <= i - j && i + j + 1 < n))
            break;
          swapp(b, i - j, i + j + 1);
          if (!dp[loop].count(b)) {
            dp[loop][b] = state.second + 1;
            q.push(make_pair(b, state.second + 1));
          }
        }
      }
    }
  }
  int res = 9;
  for (auto it = dp[0].begin(); it != dp[0].end(); it++) {
    if (!(dp[1].count(it->first)))
      continue;
    res = min(res, (it->second) + (dp[1].find(it->first)->second));
  }
  cout << res << endl;
  return 0;
}

Error: -11

Fixed submission:

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

// define
#define ALL(a) a.begin(), a.end()
#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define debug(x)                                                               \\
  if (1)                                                                       \\
    cout << #x << ":" << x << endl;
#define DEBUG(x)                                                               \\
  if (1)                                                                       \\
    cout << #x << ":" << x << endl;
#define ll long long

// constant
const int MOD = 1000000009;
const int INF = INT_MAX / 3;

// typedef
typedef vector<int> vi;
typedef vector<vector<int>> vvi;

string dfs(int now, int n, int a, int s, int g, vector<int> been,
           const vector<int> &isgo, const vector<vector<string>> &edge) {
  if (now == g)
    return "";
  if (been[now])
    return "~";
  been[now] = 1;
  vector<int> wigo;
  string kouho = "~";
  for (int i = 0; i < n; i++)
    if (isgo[i] && edge[now][i] < kouho)
      kouho = edge[now][i];
  for (int i = 0; i < n; i++)
    if (isgo[i] && edge[now][i] == kouho)
      wigo.push_back(i);
  string res = "~";
  for (int i = 0; i < wigo.size(); i++)
    res = min(res, dfs(wigo[i], n, a, s, g, been, isgo, edge));

  return kouho + res;
}
int main() {
  int n, a, s, g;
  while (cin >> n >> a >> s >> g, n) {
    vector<vector<string>> edge(n, vector<string>(n, "~"));
    REP(i, a) {
      int x, y;
      string buf;
      cin >> x >> y >> buf;
      edge[x][y] = min(edge[x][y], buf);
    }

    vector<int> isgo(n, 0);
    isgo[g] = 1;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (isgo[j]) {
          for (int r = 0; r < n; r++)
            if (edge[r][j] != "~")
              isgo[r] = 1;
        }
    if (!isgo[s]) {
      cout << "NO" << endl;
      continue;
    }
    int now = s;
    vector<int> been(n, 0);

    string res = dfs(now, n, a, s, g, been, isgo, edge);
    bool is = true;
    for (int i = 0; i < res.size(); i++)
      if (res[i] == '~') {
        is = false;
      }

    if (is)
      cout << res << endl;
    else
      cout << "NO" << endl;
  }

  return 0;
}