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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<vector<int>> dl(n, vector<int>(n, 1e8));
    vector<vector<int>> ds(n, vector<int>(n, 1e8));
    REP(i, n) {
      dl[i][i] = 0;
      ds[i][i] = 0;
    }
    REP(i, m) {
      int u, v, t;
      char c;
      cin >> u >> v >> t >> c;
      u--;
      v--;
      if (c == 'L') {
        dl[u][v] = t;
        dl[v][u] = t;
      } else {
        ds[u][v] = t;
        ds[v][u] = t;
      }
    }
    REP(i, n)
    REP(j, n)
    REP(k, n) {
      dl[j][k] = min(dl[j][k], dl[j][i] + dl[i][k]);
      ds[j][k] = min(ds[j][k], ds[j][i] + ds[i][k]);
    }
    int r;
    cin >> r;
    vector<int> goal(r);
    REP(i, r) {
      cin >> goal[i];
      goal[i]--;
    }
    vector<vector<int>> dp(r, vector<int>(n, 1e8));
    dp[0][goal[0]] = 0;
    FOR(i, 1, r) {
      REP(j, n) {
        REP(k, n) {
          dp[i][k] = min(dp[i][k], dp[i - 1][j] + dl[goal[i - 1]][j] +
                                       ds[j][k] + dl[k][goal[i]]);
          if (j == k)
            dp[i][k] = min(dp[i][k], dp[i - 1][j] + dl[goal[i - 1]][goal[i]]);
        }
      }
    }
    int ans = 1e8;
    REP(i, n)
    ans = min(ans, dp[r - 1][i]);
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

struct e {
  int to;
  int cost;
  char c;
};

struct state {
  int from; //????????´???
  int d;    //??????????????°
  int ship; //????????´???
  int cost; //????????§????????????
};

bool operator<(const state &left, const state &right) {
  return left.cost > right.cost;
}

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<vector<e>> edge(n);

    REP(i, m) {
      int u, v, t;
      char c;
      cin >> u >> v >> t >> c;
      u--;
      v--;
      edge[u].push_back({v, t, c});
      edge[v].push_back({u, t, c});
    }

    int r;
    cin >> r;
    vector<int> goal(n);
    REP(i, r) {
      cin >> goal[i];
      goal[i]--;
    }
    int ans = 0;
    //
    vector<vector<vector<int>>> memo(
        n, vector<vector<int>>(r, vector<int>(n, 1e9)));
    memo[0][0][0] = 0;
    priority_queue<state> P;
    P.push({0, 0, 0, 0});
    while (!P.empty()) {
      state st = P.top();
      P.pop();
      if (st.from == goal[st.d])
        st.d++;
      if (st.d == r) {
        ans = st.cost;
        break;
      }
      REP(i, edge[st.from].size()) {
        if (edge[st.from][i].c == 'L') {
          if (memo[edge[st.from][i].to][st.d][st.ship] >
              st.cost + edge[st.from][i].cost) {
            memo[edge[st.from][i].to][st.d][st.ship] =
                st.cost + edge[st.from][i].cost;
            P.push({edge[st.from][i].to, st.d, st.ship,
                    st.cost + edge[st.from][i].cost});
          }
        } else {
          if (memo[edge[st.from][i].to][st.d][edge[st.from][i].to] >
                  st.cost + edge[st.from][i].cost &&
              st.from == st.ship) {
            memo[edge[st.from][i].to][st.d][edge[st.from][i].to] =
                st.cost + edge[st.from][i].cost;
            P.push({edge[st.from][i].to, st.d, edge[st.from][i].to,
                    st.cost + edge[st.from][i].cost});
          }
        }
      }
    }
    cout << ans << endl;
  }
}