Problem p01166 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int readtime() {
  int hh, mm;
  scanf("%d:%d", &hh, &mm);
  return 60 * hh + mm;
}

int main() {
  for (int n, m; scanf("%d%d", &n, &m), n;) {
    int s;
    scanf("%d", &s);
    s--;
    int s_time = readtime();
    int g;
    scanf("%d", &g);
    g--;
    int g_time = readtime();

    vector<pair<int, int>> seq[100]; // seq[i] := ( 電車 i の運行予定 )
    rep(i, m) {
      int sz;
      scanf("%d", &sz);
      seq[i].resize(sz);
      rep(j, sz) {
        int u;
        scanf("%d", &u);
        u--;
        seq[i][j] = make_pair(readtime(), u);
      }
      sort(seq[i].begin(), seq[i].end());
    }

    // train[t][u] := ( 時刻 t に駅 u から発車する電車の情報 )
    static vector<pair<int, int>> train[1440][1000];
    rep(t, 1440) rep(u, n) train[t][u].clear();
    rep(i, m) {
      rep(j, seq[i].size()) {
        int t = seq[i][j].first;
        int u = seq[i][j].second;
        train[t][u].push_back(make_pair(i, j));
      }
    }

    static int dp[1440][1000];
    rep(t, 1440) rep(u, n) dp[t][u] = -1; // -1 は到達不可能の意味
    dp[s_time][s] = 0;
    rep(t, 1439) rep(u, n) if (dp[t][u] != -1) {
      dp[t + 1][u] = max(dp[t + 1][u], dp[t][u]); // 電車に乗らずにその駅で待つ
      rep(i, train[t][u].size()) {                // 電車に乗る
        int id = train[t][u][i].first;
        for (int j = train[t][u][i].second + 1; j < seq[id].size(); j++) {
          int t_next = seq[id][j].first;
          int u_next = seq[id][j].second;
          dp[t_next][u_next] =
              max(dp[t_next][u_next], max(dp[t][u], t_next - t));
        }
      }
    }

    int ans = -1;
    rep(t, g_time + 1) ans = max(ans, dp[t][g]);
    if (ans == -1)
      puts("impossible");
    else
      printf("%d\\n", ans);
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int readtime() {
  int hh, mm;
  scanf("%d:%d", &hh, &mm);
  return 60 * hh + mm;
}

int main() {
  for (int n, m; scanf("%d%d", &n, &m), n;) {
    int s;
    scanf("%d", &s);
    s--;
    int s_time = readtime();
    int g;
    scanf("%d", &g);
    g--;
    int g_time = readtime();

    static vector<pair<int, int>> train[1440][1000];
    rep(t, 1440) rep(u, n) train[t][u].clear();
    rep(i, m) {
      int sz;
      scanf("%d", &sz);
      vector<pair<int, int>> seq(sz); // 電車 i の運行予定
      rep(j, sz) {
        int u;
        scanf("%d", &u);
        u--;
        seq[j] = make_pair(readtime(), u);
      }
      sort(seq.begin(), seq.end());

      rep(k, seq.size()) rep(j, k) {
        int t0 = seq[j].first;
        int u0 = seq[j].second;
        int t1 = seq[k].first;
        int u1 = seq[k].second;
        train[t0][u0].push_back(make_pair(t1, u1));
      }
    }

    static int dp[1440][1000];
    rep(t, 1440) rep(u, n) dp[t][u] = -1; // -1 は到達不可能の意味
    dp[s_time][s] = 0;
    rep(t, 1439) rep(u, n) if (dp[t][u] != -1) {
      dp[t + 1][u] = max(dp[t + 1][u], dp[t][u]); // 電車に乗らずにその駅で待つ
      rep(i, train[t][u].size()) {                // 電車 i に乗る
        int t_next = train[t][u][i].first;
        int u_next = train[t][u][i].second;
        dp[t_next][u_next] = max(dp[t_next][u_next], max(dp[t][u], t_next - t));
      }
    }

    int ans = -1;
    rep(t, g_time + 1) ans = max(ans, dp[t][g]);
    if (ans == -1)
      puts("impossible");
    else
      printf("%d\\n", ans);
  }

  return 0;
}