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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
const int MAX_N = 210;
const int MAX_R = 1010;
const int INF = 1e8;

int main() {
  int N, M;
  while (cin >> N >> M && N) {
    int L[MAX_N][MAX_N], S[MAX_N][MAX_N], dp[MAX_R][MAX_N], v[MAX_R];
    REP(i, MAX_N) REP(j, MAX_N) L[i][j] = S[i][j] = (i == j ? 0 : INF);
    REP(i, MAX_R) REP(j, MAX_N) dp[i][j] = INF;
    REP(i, M) {
      int a, b, c;
      char t;
      cin >> a >> b >> c >> t;
      --a;
      --b;
      if (t == 'S')
        S[a][b] = S[b][a] = c;
      else
        L[a][b] = L[b][a] = c;
    }
    REP(k, N) REP(i, N) REP(j, N) L[i][j] = min(L[i][j], L[i][k] + L[k][j]);
    REP(k, N) REP(i, N) REP(j, N) S[i][j] = min(S[i][j], S[i][k] + S[k][j]);
    int R;
    cin >> R;
    REP(i, R) {
      cin >> v[i];
      --v[i];
    }
    REP(j, MAX_N) dp[0][j] = min(INF, S[v[0]][j] + L[j][v[0]]);
    FOR(i, 1, R) {
      REP(j, N) {
        REP(k, N) {
          dp[i][j] = min(dp[i][j],
                         dp[i - 1][k] + L[v[i - 1]][k] + S[k][j] + L[j][v[i]]);
          if (k == j)
            dp[i][j] = min(dp[i][j], dp[i - 1][j] + L[v[i - 1]][v[i]]);
        }
      }
    }
    int ans = INF;
    REP(i, N) ans = min(ans, dp[R - 1][i]);
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
const int MAX_N = 210;
const int MAX_R = 1010;
const int INF = 1e9 + 7;

class C {
public:
  int t, n, s, c;
  C(int _t, int _n, int _s, int _c) {
    t = _t;
    n = _n;
    s = _s;
    c = _c;
  }
  bool operator>(const C &_) const { return t != _.t ? t > _.t : c < _.c; }
};

int closed[MAX_N][MAX_N][MAX_R];

int solve(int G[MAX_N][MAX_N][2], vector<int> &v, int N, int R) {
  priority_queue<C, vector<C>, greater<C>> open;
  open.push(C(0, 0, 0, 1));
  REP(i, MAX_N) REP(j, MAX_N) REP(k, MAX_R) closed[i][j][k] = INF;
  closed[v[0]][v[0]][0] = 0;
  while (!open.empty()) {
    C tmp = open.top();
    open.pop();
    int n = tmp.n, t = tmp.t, s = tmp.s, c = tmp.c;

    REP(next, N) {
      REP(f, 2) {
        int cost = G[n][next][f];
        if (cost == INF)
          continue;
        int nc = (v[c] == next ? c + 1 : c), nt = t + cost, ns = (f ? next : s);
        if ((f && s != n) || closed[next][ns][nc] <= nt)
          continue;
        closed[next][ns][nc] = nt;
        if (nc < v.size())
          open.push(C(nt, next, ns, nc));
        open.push(C(nt, next, ns, nc));
      }
    }
  }
  int ans = INF;
  REP(i, N) ans = min(ans, closed[v[N - 1]][i][R]);
  return ans;
}

int main() {
  int N, M;
  while (cin >> N >> M && N) {
    int G[MAX_N][MAX_N][2];
    REP(i, MAX_N) REP(j, MAX_N) REP(k, 2) G[i][j][k] = INF;
    REP(i, M) {
      int f, t, c;
      char a;
      cin >> f >> t >> c >> a;
      --f;
      --t;
      int b = a == 'L' ? 0 : 1;
      G[f][t][b] = min(G[f][t][b], c);
      G[t][f][b] = min(G[t][f][b], c);
    }
    int R;
    cin >> R;
    vector<int> v(R);
    REP(i, R) {
      cin >> v[i];
      --v[i];
    }
    cout << solve(G, v, N, R) << endl;
  }
  return 0;
}