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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int main() {
  int n, m;
  vector<int> move(3);
  cin >> n >> m >> move[0] >> move[1] >> move[2];
  SORT(move);
  vector<vector<int>> route(n + 1);

  REP(i, m) {
    int a, b;
    cin >> a >> b;
    route[a].push_back(b);
  }
  int ans = -1;
  vector<vector<vector<bool>>> step(
      n + 1, vector<vector<bool>>(n + 1, vector<bool>(3, false)));
  FOR(i, 1, n) {
    vector<int> now;
    now.push_back(i);
    REP(j, move[2]) {
      int ns = now.size();
      vector<int> now2;
      set<int> si;
      REP(k, ns) {
        REP(l, route[now[k]].size()) {
          if (si.find(route[now[k]][l]) == si.end()) {
            now2.push_back(route[now[k]][l]);
            si.insert(route[now[k]][l]);
          }
        }
      }
      now = now2;
      if (j == move[0] - 1)
        REP(k, now.size())
      step[i][now[k]][0] = true;
      if (j == move[1] - 1)
        REP(k, now.size())
      step[i][now[k]][1] = true;
      if (j == move[2] - 1)
        REP(k, now.size())
      step[i][now[k]][2] = true;
    }
  }
  vector<vector<int>> goalto(n + 1, vector<int>(3, 10000));
  goalto[n][0] = 0;
  goalto[n][1] = 0;
  goalto[n][2] = 0;
  vector<bool> memo(n + 1, false);
  memo[n] = true;
  queue<pair<int, int>> qii;
  qii.push({0, n});
  while (!qii.empty()) {
    pair<int, int> num = qii.front();
    qii.pop();
    REP(i, n) {
      REP(j, 3) {
        if (step[i][num.second][j]) {
          if (goalto[i][j] == 10000) {
            goalto[i][j] = num.first + 1;
            if (!memo[i] && goalto[i][0] != 10000 && goalto[i][1] != 10000 &&
                goalto[i][2] != 10000) {
              qii.push({max(goalto[i][0], max(goalto[i][1], goalto[i][2])), i});
              memo[i] = true;
            }
          }
        }
      }
    }
  }

  REP(i, 3) {
    if (goalto[1][i] == 10000) {
      ans = -1;
      break;
    }
    ans = max(ans, goalto[1][i]);
  }
  if (ans != -1)
    cout << ans << endl;
  else
    cout << "IMPOSSIBLE" << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int main() {
  int n, m;
  vector<int> move(3);
  cin >> n >> m >> move[0] >> move[1] >> move[2];
  SORT(move);
  vector<vector<int>> route(n + 1);

  REP(i, m) {
    int a, b;
    cin >> a >> b;
    route[a].push_back(b);
  }
  int ans = -1;
  vector<vector<vector<bool>>> step(
      n + 1, vector<vector<bool>>(n + 1, vector<bool>(3, false)));
  FOR(i, 1, n) {
    vector<int> now;
    now.push_back(i);
    REP(j, move[2]) {
      int ns = now.size();
      vector<int> now2;
      REP(k, ns) {
        REP(l, route[now[k]].size()) { now2.push_back(route[now[k]][l]); }
      }
      now = now2;
      if (j == move[0] - 1)
        REP(k, now.size())
      step[i][now[k]][0] = true;
      if (j == move[1] - 1)
        REP(k, now.size())
      step[i][now[k]][1] = true;
      if (j == move[2] - 1)
        REP(k, now.size())
      step[i][now[k]][2] = true;
    }
  }
  vector<vector<int>> goalto(n + 1, vector<int>(3, 10000));
  goalto[n][0] = 0;
  goalto[n][1] = 0;
  goalto[n][2] = 0;
  vector<bool> memo(n + 1, false);
  memo[n] = true;
  queue<pair<int, int>> qii;
  qii.push({0, n});
  while (!qii.empty()) {
    pair<int, int> num = qii.front();
    qii.pop();
    REP(i, n) {
      REP(j, 3) {
        if (step[i][num.second][j]) {
          if (goalto[i][j] == 10000) {
            goalto[i][j] = num.first + 1;
            if (!memo[i] && goalto[i][0] != 10000 && goalto[i][1] != 10000 &&
                goalto[i][2] != 10000) {
              qii.push({max(goalto[i][0], max(goalto[i][1], goalto[i][2])), i});
              memo[i] = true;
            }
          }
        }
      }
    }
  }

  REP(i, 3) {
    if (goalto[1][i] == 10000) {
      ans = -1;
      break;
    }
    ans = max(ans, goalto[1][i]);
  }
  if (ans != -1)
    cout << ans << endl;
  else
    cout << "IMPOSSIBLE" << endl;
}