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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;

// container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define MT(a, b, c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

typedef TIII state;
int INF;
int distS[222][222];
int distL[222][222];
int z[1111];
int DP[2][222];

int main() {
  INF = 500000000;
  while (1) {
    int n, m, r;
    cin >> n >> m;
    if (n == 0)
      return 0;
    REP(i, 222) REP(j, 222) distS[i][j] = INF;
    REP(i, 222) REP(j, 222) distL[i][j] = INF;
    REP(i, m) {
      int x, y, t;
      char sl;
      cin >> x >> y >> t >> sl;
      x--, y--;
      if (sl == 'L')
        distL[y][x] = distL[x][y] = min(distL[x][y], t);
      else
        distS[y][x] = distS[x][y] = min(distS[x][y], t);
    }
    REP(i, n) distL[i][i] = 0;
    REP(i, n) distS[i][i] = 0;
    REP(k, n) {
      REP(i, n) {
        REP(j, i) {
          distL[i][j] = distL[j][i] =
              min(distL[i][j], distL[i][k] + distL[k][j]);
          distS[i][j] = distS[j][i] =
              min(distS[i][j], distS[i][k] + distS[k][j]);
        }
      }
    }
    cin >> r;
    REP(i, r) {
      scanf("%d", &z[i]);
      z[i]--;
    }
    REP(i, 222) DP[0][i] = INF;
    DP[0][z[0]] = 0;
    FOR(i, 1, r) {
      REP(j, 222) DP[i % 2][j] = INF;
      REP(j, n) {
        REP(k, n) {
          if (j == k)
            DP[i % 2][k] =
                min(DP[i % 2][k], DP[(i + 1) % 2][j] + distL[z[i - 1]][z[i]]);
          else
            DP[i % 2][k] =
                min(DP[i % 2][k], DP[(i + 1) % 2][j] + distL[z[i - 1]][j] +
                                      distS[j][k] + distL[k][z[i]]);
        }
      }
    }
    int ret = INF;
    REP(i, n) ret = min(ret, DP[(r + 1) % 2][i]);
    cout << ret << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;

// container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define MT(a, b, c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

typedef TIII state;
int INF;
int dist[222][222];
map<int, int> S[222];
int z[1111];
map<state, int> memo;

int main() {
  INF = 1000000000;
  REP(i, 222) REP(j, 222) dist[i][j] = INF;
  while (1) {
    int n, m, r;
    cin >> n >> m;
    REP(i, 222) S[i].clear();
    if (n == 0)
      return 0;
    REP(i, m) {
      int x, y, t;
      char sl;
      cin >> x >> y >> t >> sl;
      x--, y--;
      if (sl == 'L')
        dist[y][x] = dist[x][y] = t;
      else {
        if (EXIST(S[x], y))
          S[y][x] = S[x][y] = min(S[x][y], t);
        else
          S[y][x] = S[x][y] = t;
      }
    }
    REP(i, n) dist[i][i] = 0;
    REP(k, n) {
      REP(i, n) {
        REP(j, i) {
          dist[i][j] = dist[j][i] = min(dist[i][j], dist[i][k] + dist[k][j]);
        }
      }
    }
    cin >> r;
    REP(i, r) {
      scanf("%d", &z[i]);
      z[i]--;
    }
    state st = MT(0, 0, z[0]);
    priority_queue<state, vector<state>, greater<state>> q;
    q.push(st);
    while (!q.empty()) {
      state tst = q.top();
      q.pop();
      if (EXIST(memo, tst))
        continue;
      memo[tst] = tst.first;
      if (tst.T2 == r - 1) {
        cout << tst.T1 << endl;
        break;
      }
      if (dist[z[tst.T2]][z[tst.T2 + 1]] < INF) {
        state rst =
            MT(dist[z[tst.T2]][z[tst.T2 + 1]] + tst.T1, tst.T2 + 1, tst.T3);
        q.push(rst);
      }
      int D = dist[z[tst.T2]][tst.T3] + tst.T1;
      for (map<int, int>::iterator it = S[tst.T3].begin();
           it != S[tst.T3].end(); it++) {
        if (dist[it->first][z[tst.T2 + 1]] < INF) {
          q.push(MT(D + it->second + dist[it->first][z[tst.T2 + 1]], tst.T2 + 1,
                    it->first));
        }
      }
    }
  }
}