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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pa;
const int inf = 1e8;
int mml[205][205], mms[205][205], dp[1005][205], num[1005];
int main() {
  int n, m, a, b, c, r;
  char str[5];
  while (scanf("%d%d", &n, &m) && n) {
    for (int i = 0; i < n; i++) {
      mml[i][i] = mms[i][i] = 0;
      for (int j = i + 1; j < n; j++) {
        mml[i][j] = mml[j][i] = inf;
        mms[i][j] = mms[j][i] = inf;
      }
    }
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d%s", &a, &b, &c, str);
      a--, b--;
      if (str[0] == 'L') {
        mml[a][b] = mml[b][a] = min(mml[a][b], c);
      } else {
        mms[a][b] = mms[b][a] = min(mms[a][b], c);
      }
    }
    scanf("%d", &r);
    for (int i = 0; i < r; i++) {
      scanf("%d", &num[i]);
      num[i]--;
    }
    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          mml[i][j] = min(mml[i][j], mml[i][k] + mml[k][j]);
          mms[i][j] = min(mms[i][j], mms[i][k] + mms[k][j]);
        }
      }
    }
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < n; j++) {
        dp[i][j] = inf;
      }
    }
    for (int i = 0; i < n; i++) {
      dp[0][i] = mms[num[0]][i] + mml[i][num[0]];
    }
    int ans = inf;
    for (int i = 1; i < r; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          if (j != k)
            dp[i][k] = min(dp[i][k], dp[i - 1][j] + mml[num[i - 1]][j] +
                                         mms[j][k] + mml[k][num[i]]);
          else
            dp[i][j] = min(dp[i][j], dp[i - 1][j] + mml[num[i - 1]][num[i]]);
        }
      }
    }
    for (int i = 0; i < n; i++) {
      ans = min(ans, dp[r - 1][i]);
    }
    printf("%d\\n", ans);
  }
  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;
}