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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

const int INF = (1 << 29);

class State {
public:
  int d, n, s;
  State(int d = 0, int n = 0, int s = 0) : d(d), n(n), s(s) {}
  bool operator<(const State &st) const { return d > st.d; }
};

class State2 {
public:
  int d;
  bool f;
  State2(int d = INF, bool f = false) : d(d), f(f) {}
};

int main() {
  int n, m;
  while (cin >> n >> m && n && m) {
    vector<vector<int>> L(n, vector<int>(n, INF)), S(n, vector<int>(n, INF));
    for (int i = 0; i < m; i++) {
      int a, b;
      int c;
      char d;
      cin >> a >> b >> c >> d;
      a--;
      b--;
      if (d == 'L')
        L[a][b] = L[b][a] = c;
      else
        S[a][b] = S[b][a] = c;
    }

    for (int k = 0; k < n; k++) {
      L[k][k] = S[k][k] = 0;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          for (int l = 0; l < 2; l++) {
            if (l == 0) {
              if (L[i][k] >= INF || L[k][j] >= INF)
                continue;
              L[i][j] = min(L[i][j], L[i][k] + L[k][j]);
            } else {
              if (S[i][k] >= INF || S[k][j] >= INF)
                continue;
              S[i][j] = min(S[i][j], S[i][k] + S[k][j]);
            }
          }
        }
      }
    }

    int n2;
    cin >> n2;
    vector<int> dest(n2);
    for (int i = 0; i < n2; i++) {
      cin >> dest[i];
      dest[i]--;
    }

    vector<vector<int>> dp(2, vector<int>(n, INF));
    dp[0][dest[0]] = 0;

    for (int i = 1; i < n2; i++) {
      for (int j = 0; j < n; j++) {
        if (dp[0][j] >= INF || L[dest[i - 1]][j] >= INF)
          continue;
        for (int k = 0; k < n; k++) {
          if (S[j][k] >= INF || L[k][dest[i]] >= INF)
            continue;
          dp[1][k] = min(dp[1][k], dp[0][j] + L[dest[i - 1]][j] + S[j][k] +
                                       L[k][dest[i]]);
          if (j == k)
            dp[1][k] = min(dp[1][k], dp[0][j] + L[dest[i - 1]][dest[i]]);
        }
      }
      dp[0].assign(dp[1].begin(), dp[1].end());
      dp[1].assign(n, INF);
    }

    int ans = INF;
    for (int i = 0; i < n; i++)
      ans = min(ans, dp[0][i]);

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

const int INF = (1 << 21);

class State {
public:
  int d, n, s;
  State(int d = 0, int n = 0, int s = 0) : d(d), n(n), s(s) {}
  bool operator<(const State &st) const { return d > st.d; }
};

class State2 {
public:
  int d;
  bool f;
  State2(int d = INF, bool f = false) : d(d), f(f) {}
};

int main() {
  int n, m;
  while (cin >> n >> m && n && m) {
    vector<vector<int>> L(n, vector<int>(n, INF)), S(n, vector<int>(n, INF));
    for (int i = 0; i < m; i++) {
      int a, b, c;
      char d;
      cin >> a >> b >> c >> d;
      a--;
      b--;
      if (d == 'L')
        L[a][b] = L[b][a] = c;
      else
        S[a][b] = S[b][a] = c;
    }

    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          for (int l = 0; l < 2; l++) {
            if (l == 0) {
              if (L[i][k] >= INF || L[k][j] >= INF)
                continue;
              L[i][j] = min(L[i][j], L[i][k] + L[k][j]);
            } else {
              if (S[i][k] >= INF || S[k][j] >= INF)
                continue;
              S[i][j] = min(S[i][j], S[i][k] + S[k][j]);
            }
          }
        }
      }
    }

    int n2;
    cin >> n2;
    vector<int> dest(n2);
    for (int i = 0; i < n2; i++) {
      cin >> dest[i];
      dest[i]--;
    }

    vector<vector<int>> ans(2, vector<int>(n, INF));
    ans[0][dest[0]] = 0;
    int ans2;

    for (int i = 1; i < n2; i++) {
      ans2 = INF;
      for (int j = 0; j < n; j++) {
        if (ans[0][j] >= INF)
          continue;

        priority_queue<State> Q;
        vector<vector<State2>> d(n, vector<State2>(n));
        State u(ans[0][j], dest[i - 1], j);
        d[dest[i - 1]][j].d = ans[0][j];
        Q.push(u);

        while (!Q.empty()) {
          u = Q.top();
          Q.pop();

          if (d[u.n][u.s].f)
            continue;
          d[u.n][u.s].f = true;
          if (u.n == dest[i]) {
            ans[1][u.s] = min(ans[1][u.s], u.d);
            ans2 = min(ans2, ans[1][u.s]);
            continue;
          }

          for (int k = 0; k < n; k++) {
            if (!d[k][u.s].f && d[u.n][u.s].d + L[u.n][k] < d[k][u.s].d) {
              State v = u;
              v.d = d[k][u.s].d = d[u.n][u.s].d + L[u.n][k];
              v.n = k;
              Q.push(v);
            }
            if (u.s == u.n) {
              if (!d[k][k].f && d[u.n][u.s].d + S[u.n][k] < d[k][k].d) {
                State v2 = u;
                v2.d = d[k][k].d = d[u.n][u.s].d + S[u.n][k];
                v2.s = v2.n = k;
                Q.push(v2);
              }
            }
          }
        }
      }
      ans[0].assign(ans[1].begin(), ans[1].end());
      ans[1].assign(n, INF);
    }

    cout << ans2 << endl;
  }
  return 0;
}