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

#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

const int INF = 1 << 20;

// ティツ。ツ古・ツ按療」ツδゥテ」ツつ、テ」ツδ姪」ツδゥテ」ツδェ
// Verified: TopCoder SRM 704 Div.2 (ModEquationEasy)
// ティツ。ツ古・ツ按療」ツ?ョテァツゥツ?テ」ツ?ィ
// テァツエツッテ、ツケツ?テァツケツーテ」ツつ甘ィツソツ氾」ツ?療、ツコツ古、ツケツ療ヲツウツ?

// Matrix Library Begin (C++11)

template <typename T> using Matrix = vector<vector<T>>;

template <typename T> void init_mat(Matrix<T> &A, int h, int w) {
  A.resize(h, vector<T>(w, 0));
}

template <typename T> Matrix<T> calc_mat(Matrix<T> A, Matrix<T> B) {
  Matrix<T> C(A.size(), vector<T>(B[0].size()));
  for (int i = 0; i < A.size(); i++) {
    for (int k = 0; k < B.size(); k++) {
      for (int j = 0; j < B[0].size(); j++) {
        C[i][j] |= A[i][k] & B[k][j]; // modテ」ツ?ェテ」ツ??
        // C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD; // modテ」ツ?づ」ツつ?
      }
    }
  }
  return C;
}

template <typename T> Matrix<T> mat_pow(Matrix<T> A, int n) {
  Matrix<T> B(A.size(), vector<T>(A.size()));
  for (int i = 0; i < A.size(); i++)
    B[i][i] = 1;
  while (n > 0) {
    if (n & 1)
      B = calc_mat(B, A);
    A = calc_mat(A, A);
    n >>= 1;
  }
  return B;
}

// Matrix Library End

Matrix<int> trans(Matrix<int> A) {
  int N = A.size();
  Matrix<int> ret;
  init_mat(ret, N, N);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      ret[i][j] = A[j][i];
    }
  }
  return ret;
}

int main() {
  int N, M, A, B, C;
  scanf("%d%d%d%d%d", &N, &M, &A, &B, &C);

  Matrix<int> adj;
  init_mat(adj, N, N);
  for (int i = 0; i < M; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v--;
    adj[u][v]++;
  }

  Matrix<int> gs[3], gg;
  gs[0] = mat_pow(adj, A), gs[1] = mat_pow(adj, B), gs[2] = mat_pow(adj, C);
  for (int i = 0; i < 3; i++)
    gs[i] = trans(gs[i]);

  int cnt = 0;
  bool checked[55] = {};
  queue<int> que;
  que.push(N - 1);
  checked[N - 1] = true;
  while (1) {
    queue<int> tmp;
    cnt++;
    bool cont = false;

    Matrix<int> mat;
    init_mat(mat, 3, N);
    while (!que.empty()) {
      int cur = que.front();
      // printf("cnt = %d, cur = %d\\n", cnt, cur);
      tmp.push(cur);
      que.pop();
      for (int to = 0; to < N; to++) {
        for (int k = 0; k < 3; k++) {
          mat[k][to] |= gs[k][cur][to];
        }
      }
    }

    vector<int> res(N, 1);
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < 3; k++) {
        res[j] &= mat[k][j];
      }
      if (res[j]) {
        if (checked[j])
          continue;
        if (j == 0) {
          printf("%d\\n", cnt);
          return 0;
        }
        checked[j] = true;
        cont = true;
        tmp.push(j);
      }
    }

    if (!cont)
      break;
    que = tmp;
  }
  printf("IMPOSSIBLE\\n");
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

const int INF = 1 << 20;

// ティツ。ツ古・ツ按療」ツδゥテ」ツつ、テ」ツδ姪」ツδゥテ」ツδェ
// Verified: TopCoder SRM 704 Div.2 (ModEquationEasy)
// ティツ。ツ古・ツ按療」ツ?ョテァツゥツ?テ」ツ?ィ
// テァツエツッテ、ツケツ?テァツケツーテ」ツつ甘ィツソツ氾」ツ?療、ツコツ古、ツケツ療ヲツウツ?

// Matrix Library Begin (C++11)

template <typename T> using Matrix = vector<vector<T>>;

template <typename T> void init_mat(Matrix<T> &A, int h, int w) {
  A.resize(h, vector<T>(w, 0));
}

template <typename T> Matrix<T> calc_mat(Matrix<T> A, Matrix<T> B) {
  Matrix<T> C(A.size(), vector<T>(B[0].size()));
  for (int i = 0; i < A.size(); i++) {
    for (int k = 0; k < B.size(); k++) {
      for (int j = 0; j < B[0].size(); j++) {
        C[i][j] |= A[i][k] & B[k][j]; // modテ」ツ?ェテ」ツ??
        // C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD; // modテ」ツ?づ」ツつ?
      }
    }
  }
  return C;
}

template <typename T> Matrix<T> mat_pow(Matrix<T> A, int n) {
  Matrix<T> B(A.size(), vector<T>(A.size()));
  for (int i = 0; i < A.size(); i++)
    B[i][i] = 1;
  while (n > 0) {
    if (n & 1)
      B = calc_mat(B, A);
    A = calc_mat(A, A);
    n >>= 1;
  }
  return B;
}

// Matrix Library End

Matrix<int> trans(Matrix<int> A) {
  int N = A.size();
  Matrix<int> ret;
  init_mat(ret, N, N);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      ret[i][j] = A[j][i];
    }
  }
  return ret;
}

int main() {
  int N, M, A, B, C;
  scanf("%d%d%d%d%d", &N, &M, &A, &B, &C);

  Matrix<int> adj;
  init_mat(adj, N, N);
  for (int i = 0; i < M; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v--;
    adj[u][v]++;
  }

  Matrix<int> gs[3], gg;
  gs[0] = mat_pow(adj, A), gs[1] = mat_pow(adj, B), gs[2] = mat_pow(adj, C);
  for (int i = 0; i < 3; i++)
    gs[i] = trans(gs[i]);

  vector<int> goals;
  goals.push_back(N - 1);

  int cnt = 0;
  bool checked[55] = {};
  queue<int> que, tmp;
  que.push(N - 1);
  checked[N - 1] = true;
  while (!que.empty()) {
    cnt++;
    while (!que.empty()) {
      int cur = que.front();
      que.pop();
      for (int nxt = 0; nxt < N; nxt++) {
        bool ok = true;
        for (int k = 0; k < 3; k++) {
          ok &= gs[k][cur][nxt];
        }
        if (ok) {
          if (nxt == 0) {
            printf("%d\\n", cnt);
            return 0;
          }
          if (!checked[nxt]) {
            tmp.push(nxt);
            checked[nxt] = true;
          }
        }
      }
    }
    que = tmp;
  }
  printf("IMPOSSIBLE\\n");
  return 0;
}