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

#include <bits/stdc++.h>
using namespace std;

int N, M, S, T;
int st, ed;

unordered_map<int, vector<int>> R;
unordered_map<int, vector<int>> tr;

int ghs(int a, int b) { return a * 24 * 60 + b; }

int gettime(int t1, int t2) { return t1 * 60 + t2; }

const int INF = (1 << 29);
typedef pair<int, int> P;
P dp[60 * 24 + 1][1101];
void update(P &a, P b) {
  if (a.first < b.first)
    a.first = b.first;
  if (a.second < b.second)
    a.second = b.second;
  if (a.first < a.second)
    a.first = a.second;
}
int solve() {
  fill(dp[0], dp[ed + 1], P(-INF, -INF));
  dp[st][S + M] = P(0, 0);
  for (int t = st; t <= ed; t++) {
    for (int i = 0; i < N + M; i++) {
      int id = i % (N + M);
      if (dp[t][id].first == -INF)
        continue;
      if (id < M) { // train
        if (R.count(ghs(id, t)))
          for (int nid : R[ghs(id, t)])
            update(dp[t][nid + M], P(dp[t][id].first, 0));
        update(dp[t + 1][id], P(dp[t][id].first, dp[t][id].second + 1));
      } else { // station
        int x = id - M;
        if (tr.count(ghs(x, t)))
          for (int nid : tr[ghs(x, t)])
            update(dp[t + 1][nid], P(dp[t][id].first, 1));
        update(dp[t + 1][id], P(dp[t][id].first, 0));
      }
    }
  }
  return dp[ed][T + M].first;
}

int main() {
  while (~scanf("%d%d", &N, &M) && (N | M)) {
    int t1, t2, t3, t4;
    scanf("%d %d:%d %d %d:%d", &S, &t1, &t2, &T, &t3, &t4);
    --S;
    --T;
    st = gettime(t1, t2), ed = gettime(t3, t4);

    for (int i = 0; i < M; i++) {
      int n;
      scanf("%d", &n);
      for (int j = 0; j < n; j++) {
        int k;
        scanf("%d %d:%d", &k, &t1, &t2);
        --k;
        R[ghs(i, gettime(t1, t2))].emplace_back(k);
        tr[ghs(k, gettime(t1, t2))].emplace_back(i);
        // cout << i << " "<< gettime(t1,t2) << " " << R[i][gettime(t1,t2)] <<
        // endl;
        //  cout << k << " "<< gettime(t1,t2) << " " << tr[k][gettime(t1,t2)] <<
        //  endl;
      }
    }

    int res = solve();
    if (res < 0)
      printf("impossible\\n");
    else
      printf("%d\\n", res);
    tr.clear();
    R.clear();
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int N, M, S, T;
int st, ed;
vector<int> R[101][60 * 24 + 1];
vector<int> tr[1001][60 * 24 + 1];

int gettime(int t1, int t2) { return t1 * 60 + t2; }

const int INF = (1 << 29);
typedef pair<int, int> P;
P dp[60 * 24 + 1][1101];
vector<int> pr[60 * 24 + 1][1101];
void update(P &a, P b) {
  if (a.first < b.first)
    a.first = b.first;
  if (a.second < b.second)
    a.second = b.second;
  if (a.first < a.second)
    a.first = a.second;
}
int solve() {
  fill(dp[0], dp[ed + 1], P(-INF, -INF));
  dp[st][S + M] = P(0, 0);
  for (int t = st; t <= ed; t++) {
    for (int i = 0; i < N + M; i++) {
      int id = i % (N + M);
      if (dp[t][id].first == -INF)
        continue;
      if (id < M) { // train
        for (int nid : R[id][t])
          update(dp[t][nid + M], P(dp[t][id].first, 0));
        update(dp[t + 1][id], P(dp[t][id].first, dp[t][id].second + 1));
      } else { // station
        int x = id - M;
        for (int nid : tr[x][t])
          update(dp[t + 1][nid], P(dp[t][id].first, 1));
        update(dp[t + 1][id], P(dp[t][id].first, 0));
      }
    }
  }
  return dp[ed][T + M].first;
}

int main() {
  while (~scanf("%d%d", &N, &M) && (N | M)) {
    int t1, t2, t3, t4;
    scanf("%d %d:%d %d %d:%d", &S, &t1, &t2, &T, &t3, &t4);
    --S;
    --T;
    st = gettime(t1, t2), ed = gettime(t3, t4);

    for (int i = 0; i < M; i++) {
      int n;
      scanf("%d", &n);
      for (int j = 0; j < n; j++) {
        int k;
        scanf("%d %d:%d", &k, &t1, &t2);
        --k;
        R[i][gettime(t1, t2)].emplace_back(k);
        tr[k][gettime(t1, t2)].emplace_back(i);
        // cout << i << " "<< gettime(t1,t2) << " " << R[i][gettime(t1,t2)] <<
        // endl;
        //  cout << k << " "<< gettime(t1,t2) << " " << tr[k][gettime(t1,t2)] <<
        //  endl;
      }
    }

    int res = solve();
    if (res < 0)
      printf("impossible\\n");
    else
      printf("%d\\n", res);
    for (int j = 0; j < N; j++)
      for (int i = 0; i <= 60 * 24; i++)
        tr[j][i].clear();
    for (int j = 0; j < M; j++)
      for (int i = 0; i <= 60 * 24; i++)
        R[j][i].clear();
  }
}