Problem p00937 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <complex>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

const int INF = 1 << 29;
const double EPS = 1e-8;
typedef vector<int> vec;
typedef pair<int, int> P;
typedef long long ll;

vector<vec> G;
vector<bool> goal;

bool memo[50][1001];
bool move(int v, int t) {
  if (memo[v][t])
    return true;
  if (t == 0) {
    return goal[v];
  } else {
    if (G[v].size() == 0)
      return false;
    for (int i = 0; i < G[v].size(); i++) {
      int to = G[v][i];
      if (move(to, t - 1)) {
        memo[v][t] = true;
        return true;
      }
    }
    return false;
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vec abc(3);
  cin >> abc[0] >> abc[1] >> abc[2];

  G.assign(n, vec(0));
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    G[u - 1].push_back(v - 1);
  }

  bitset<50> cango[50][1001];
  for (int j = 0; j < n; j++) {
    cango[j][0].set(j);
    for (int k = 0; k < G[j].size(); k++) {
      int to = G[j][k];
      cango[j][1].set(to);
    }
  }
  for (int i = 2; i <= 1000; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < G[j].size(); k++) {
        int to = G[j][k];
        cango[j][i] |= cango[to][i - 1];
      }
    }
  }

  goal.assign(n, false);
  goal[n - 1] = true;
  for (int t = 1; t <= n; t++) {
    vector<bool> goal_new = goal;
    for (int i = 0; i < n; i++) {
      if (goal[i])
        continue;
      bool able = true;
      for (int j = 0; j < 3; j++) {
        bool ableany = false;
        for (int k = 0; k < n; k++) {
          if (!goal[k])
            continue;
          if (goal[k] && cango[i][abc[j]][k]) {
            ableany = true;
            break;
          }
        }
        if (!ableany) {
          able = false;
          break;
        }
      }
      if (able) {
        if (i == 0) {
          cout << t << endl;
          return 0;
        }
        goal_new[i] = true;
      }
    }
    goal = goal_new;
  }

  cout << "IMPOSSIBLE" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

const int INF = 1 << 29;
const double EPS = 1e-8;
typedef vector<int> vec;
typedef pair<int, int> P;
typedef long long ll;

vector<vec> G;
vector<bool> goal;

bool memo[50][1001];
bool move(int v, int t) {
  if (memo[v][t])
    return true;
  if (t == 0) {
    return goal[v];
  } else {
    if (G[v].size() == 0)
      return false;
    for (int i = 0; i < G[v].size(); i++) {
      int to = G[v][i];
      if (move(to, t - 1)) {
        memo[v][t] = true;
        return true;
      }
    }
    return false;
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vec abc(3);
  cin >> abc[0] >> abc[1] >> abc[2];

  G.assign(n, vec(0));
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    G[u - 1].push_back(v - 1);
  }

  memset(memo, 0, sizeof memo);
  goal.assign(n, false);
  goal[n - 1] = true;
  for (int t = 1; t <= n; t++) {
    vector<bool> goal_new = goal;
    for (int i = 0; i < n; i++) {
      if (goal[i])
        continue;
      bool able = true;
      for (int j = 0; j < 3; j++) {
        if (!move(i, abc[j])) {
          able = false;
          break;
        }
      }
      if (able) {
        if (i == 0) {
          cout << t << endl;
          return 0;
        }
        goal_new[i] = true;
      }
    }
    goal = goal_new;
  }

  cout << "IMPOSSIBLE" << endl;
  return 0;
}