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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// #include<cctype>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>
// #include<list>
#include <algorithm>
#include <deque>
#include <queue>
// #include<numeric>
#include <complex>
#include <utility>
// #include<memory>
#include <cassert>
#include <functional>
#include <set>
#include <stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

const int MAXN = 55;
vector<int> G[MAXN];
int dp[MAXN];
bool done[MAXN][2 * MAXN];
const int INF = 1e9;
int n, m;

void dfs(int now, int rest, set<int> &canvisit) {
  if (done[now][rest])
    return;
  done[now][rest] = true;
  if (rest == 0) {
    canvisit.insert(now);
    return;
  }
  for (int v : G[now]) {
    dfs(v, rest - 1, canvisit);
  }
}

set<int> canvisit[3][MAXN];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<int> a(3);
  cin >> n >> m >> a[0] >> a[1] >> a[2];
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G[x].push_back(y);
  }
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < n; j++) {
      memset(done, false, sizeof(done));
      dfs(j, a[i], canvisit[i][j]);
    }
  }
  for (int i = 0; i < n; i++)
    dp[i] = INF;
  dp[n - 1] = 0;
  for (int t = 0; t < n; t++) {
    for (int i = 0; i < n; i++) {
      int ans = 0;
      for (int j = 0; j < 3; j++) {
        int tmp = INF;
        for (int u : canvisit[j][i]) {
          tmp = min(tmp, 1 + dp[u]);
        }
        ans = max(ans, tmp);
      }
      dp[i] = min(dp[i], ans);
    }
  }
  if (dp[0] == INF)
    cout << "IMPOSSIBLE" << endl;
  else
    cout << dp[0] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// #include<cctype>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>
// #include<list>
#include <algorithm>
#include <deque>
#include <queue>
// #include<numeric>
#include <complex>
#include <utility>
// #include<memory>
#include <cassert>
#include <functional>
#include <set>
#include <stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

const int MAXN = 55;
vector<int> G[MAXN];

void dfs(int now, int rest, vi &canvisit) {
  if (rest == 0) {
    canvisit.push_back(now);
    return;
  }
  for (int v : G[now]) {
    dfs(v, rest - 1, canvisit);
  }
}

vi canvisit[3][MAXN];
int dp[MAXN];
const int INF = 1e9;
int n, m;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<int> a(3);
  cin >> n >> m >> a[0] >> a[1] >> a[2];
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G[x].push_back(y);
  }
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < n; j++) {
      dfs(j, a[i], canvisit[i][j]);
      sort(canvisit[i][j].begin(), canvisit[i][j].end());
      canvisit[i][j].erase(unique(canvisit[i][j].begin(), canvisit[i][j].end()),
                           canvisit[i][j].end());
    }
  }
  for (int i = 0; i < n; i++)
    dp[i] = INF;
  dp[n - 1] = 0;
  for (int t = 0; t < n; t++) {
    for (int i = 0; i < n; i++) {
      int ans = 0;
      for (int j = 0; j < 3; j++) {
        int tmp = INF;
        for (int u : canvisit[j][i]) {
          tmp = min(tmp, 1 + dp[u]);
        }
        ans = max(ans, tmp);
      }
      dp[i] = min(dp[i], ans);
    }
  }
  if (dp[0] == INF)
    cout << "IMPOSSIBLE" << endl;
  else
    cout << dp[0] << endl;
  return 0;
}