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

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define INF 1000
using namespace std;

int n, m, type[3];
vector<int> G[101];
vector<int> step[101][3];
bool used[101];
int dp[101];

void bfs(int v, int st) {
  queue<int> que[151];
  que[0].push(v);
  for (int i = 0; i < type[st]; i++) {
    memset(used, false, sizeof(used));
    while (que[i].size()) {
      int now = que[i].front();
      que[i].pop();
      for (int j = 0; j < G[now].size(); j++) {
        int nw = G[now][j];
        if (!used[nw]) {
          used[nw] = true;
          que[i + 1].push(nw);
        }
      }
    }
  }
  while (que[type[st]].size()) {
    step[v][st].push_back(que[type[st]].front());
    que[type[st]].pop();
  }
}

int func(int v) {
  if (v == n - 1)
    return 0;
  if (dp[v] >= 0)
    return dp[v];
  if (used[v])
    return INF;
  int res = 0;
  used[v] = true;
  for (int i = 0; i < 3; i++) {
    int rr = INF;
    for (int j = 0; j < step[v][i].size(); j++) {
      rr = min(rr, func(step[v][i][j]));
    }
    res = max(res, rr);
  }
  used[v] = false;
  return dp[v] = (res + 1);
}

int main(void) {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < 3; i++) {
    scanf("%d", &type[i]);
  }
  for (int i = 0; i < m; i++) {
    int f, t;
    scanf("%d%d", &f, &t);
    f--;
    t--;
    G[f].push_back(t);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      bfs(i, j);
    }
  }
  for (int i = 0; i < n; i++) {
    dp[i] = INF;
  }
  dp[n - 1] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int lr = 0;
      for (int k = 0; k < 3; k++) {
        int rr = INF;
        for (int l = 0; l < step[j][k].size(); l++) {
          rr = min(rr, dp[step[j][k][l]] + 1);
        }
        lr = max(lr, rr);
      }
      dp[j] = min(dp[j], lr);
    }
  }

  memset(used, false, sizeof(used));
  // int res=func(0);
  if (dp[0] >= INF)
    printf("IMPOSSIBLE\\n");
  else
    printf("%d\\n", dp[0]);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define INF 1000000
using namespace std;

int n, m, type[3];
vector<int> G[51];
vector<int> step[51][3];
bool used[51];
int dp[51];

void bfs(int v, int st) {
  queue<int> que[101];
  que[0].push(v);
  for (int i = 0; i < type[st]; i++) {
    memset(used, false, sizeof(used));
    while (que[i].size()) {
      int now = que[i].front();
      que[i].pop();
      for (int j = 0; j < G[now].size(); j++) {
        int nw = G[now][j];
        if (!used[nw]) {
          used[nw] = true;
          que[i + 1].push(nw);
        }
      }
    }
  }
  while (que[type[st]].size()) {
    step[v][st].push_back(que[type[st]].front());
    que[type[st]].pop();
  }
}

int func(int v) {
  if (used[v])
    return INF;
  if (v == n - 1)
    return 0;
  int res = 0;
  used[v] = true;
  for (int i = 0; i < 3; i++) {
    int rr = INF;
    for (int j = 0; j < step[v][i].size(); j++) {
      rr = min(rr, func(step[v][i][j]));
    }
    res = max(res, rr);
  }
  used[v] = false;
  return res + 1;
}

int main(void) {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < 3; i++) {
    scanf("%d", &type[i]);
  }
  for (int i = 0; i < m; i++) {
    int f, t;
    scanf("%d%d", &f, &t);
    f--;
    t--;
    G[f].push_back(t);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      bfs(i, j);
    }
  }
  memset(used, false, sizeof(used));
  int res = func(0);
  if (res >= INF)
    printf("IMPOSSIBLE\\n");
  else
    printf("%d\\n", res);
  return 0;
}