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

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define maxn (210)
#define ll long long
#define il inline
#define RG register
using namespace std;
const ll inf = 0x1f1f1f1f1f1f1f1f;
il ll gi() {
  RG ll x = 0, q = 1;
  RG char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    q = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - 48, ch = getchar();
  return q * x;
}

char s[2];
int n, m, R, Q[maxn * 5];
ll L[maxn][maxn], S[maxn][maxn], C[maxn * 5][maxn];

il ll dfs() {
  for (RG int k = 1; k <= n; k++) {
    for (RG int i = 1; i <= n; i++)
      for (RG int j = 1; j <= n; j++) {
        S[i][j] = min(S[i][j], S[i][k] + S[k][j]);
        L[i][j] = min(L[i][j], L[i][k] + L[k][j]);
      }
  }
  C[1][Q[1]] = 0;
  for (RG int i = 1; i <= R; i++)
    for (RG int j = 1; j <= n; j++) {
      C[i][j] = min(C[i][j], C[i - 1][j] + L[Q[i - 1]][Q[i]]);
      for (RG int k = 1; k <= n; k++) {
        C[i][k] =
            min(C[i][k], C[i - 1][j] + L[Q[i - 1]][j] + S[j][k] + L[k][Q[i]]);
      }
    }
  RG ll ans = inf;
  for (RG int i = 1; i <= n; i++)
    ans = min(ans, C[R][i]);
  return ans;
}

il void init() {
  memset(L, inf, sizeof(L));
  memset(S, inf, sizeof(S));
  memset(C, inf, sizeof(C));
  for (RG int i = 1; i <= n; ++i)
    L[i][i] = S[i][i] = 0;
}

int main() {
  while (scanf("%d%d", &n, &m), n | m) {
    init();
    for (int i = 1; i <= m; ++i) {
      RG int a = gi(), b = gi();
      RG ll c = gi();
      scanf("%s", s);
      if (s[0] == 'S')
        S[a][b] = S[b][a] = min(S[a][b], c);
      else
        L[a][b] = L[b][a] = min(L[a][b], c);
    }
    R = gi();
    for (RG int i = 1; i <= R; ++i)
      Q[i] = gi();
    printf("%lld\\n", dfs());
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#define ll long long
using namespace std;

const int N = 210;
const int M = 10010;

int n, m, e_num, s, ans;
int nxt[M * 2], to[M * 2], w[M * 2], c[M * 2], h[N], p[1010], dis[N][N][1010];
bool in[N][N][1010];

struct Node {
  int x, y, k;
};

queue<Node> q;

int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while (ch != '-' && (ch < '0' || ch > '9'))
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return o * x;
}

void add(int x, int y, int z, int f) {
  nxt[++e_num] = h[x], to[e_num] = y, w[e_num] = z, c[e_num] = f, h[x] = e_num;
}

void spfa() {
  memset(dis, 63, sizeof(dis));
  dis[1][1][p[1] == 1] = 0, in[1][1][p[1] == 1] = 1,
                    q.push((Node){1, 1, p[1] == 1});
  while (!q.empty()) {
    Node u = q.front();
    int x = u.x, y = u.y, k = u.k;
    in[x][y][k] = 0, q.pop();
    for (int i = h[x]; i; i = nxt[i]) {
      int v = to[i];
      if (c[i] == 0) {
        if (v == p[k + 1]) {
          if (dis[x][y][k] + w[i] < dis[v][y][k + 1]) {
            dis[v][y][k + 1] = dis[x][y][k] + w[i];
            if (!in[v][y][k + 1] && k + 1 != s)
              in[v][y][k + 1] = 1, q.push((Node){v, y, k + 1});
          }
        } else {
          if (dis[x][y][k] + w[i] < dis[v][y][k]) {
            dis[v][y][k] = dis[x][y][k] + w[i];
            if (!in[v][y][k] && k != s)
              in[v][y][k] = 1, q.push((Node){v, y, k});
          }
        }
      } else if (x == y) {
        if (v == p[k + 1]) {
          if (dis[x][y][k] + w[i] < dis[v][v][k + 1]) {
            dis[v][v][k + 1] = dis[x][y][k] + w[i];
            if (!in[v][v][k + 1] && k + 1 != s)
              in[v][v][k + 1] = 1, q.push((Node){v, v, k + 1});
          }
        } else {
          if (dis[x][y][k] + w[i] < dis[v][v][k]) {
            dis[v][v][k] = dis[x][y][k] + w[i];
            if (!in[v][v][k] && k != s)
              in[v][v][k] = 1, q.push((Node){v, v, k});
          }
        }
      }
    }
  }
}

void init() {
  e_num = 0;
  memset(h, 0, sizeof(h));
  memset(p, 0, sizeof(p));
}

int main() {
  while (scanf("%d%d", &n, &m) && n + m) {
    init();
    for (int i = 1; i <= m; i++) {
      int x = gi(), y = gi(), z = gi();
      char ch;
      scanf("%c", &ch);
      if (ch == 'L')
        add(x, y, z, 0), add(y, x, z, 0);
      else
        add(x, y, z, 1), add(y, x, z, 1);
    }
    s = gi(), ans = 1 << 30;
    for (int i = 1; i <= s; i++)
      p[i] = gi();
    spfa();
    for (int i = 1; i <= n; i++) {
      ans = min(ans, dis[p[s]][i][s]);
    }
    printf("%d\\n", ans);
  }
  return 0;
}