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

#include <bits/stdc++.h>
using namespace std;
#define int long long
int dp[1111][222];
signed main() {
  int n, m;
  while (cin >> n >> m, n) {
    int x[m], y[m], t[m];
    char sl[m];
    for (int i = 0; i < m; i++)
      cin >> x[i] >> y[i] >> t[i] >> sl[i];
    int r;
    cin >> r;
    int z[r];
    for (int i = 0; i < r; i++)
      cin >> z[i];
    int INF = 1LL << 55LL;
    int s[n][n], l[n][n];
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        s[i][j] = l[i][j] = INF * (i != j);
    for (int i = 0; i < m; i++) {
      x[i]--;
      y[i]--;
      if (sl[i] == 'S')
        s[x[i]][y[i]] = s[y[i]][x[i]] = min(s[x[i]][y[i]], t[i]);
      else
        l[x[i]][y[i]] = l[y[i]][x[i]] = min(l[x[i]][y[i]], t[i]);
    }
    for (int k = 0; k < n; k++)
      for (int i = 0; i < n; i++)
        for (int j = 0; 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]);
    for (int i = 0; i < r; i++)
      z[i]--;
    for (int i = 0; i < r; i++)
      for (int j = 0; j < n; j++)
        dp[i][j] = INF;
    dp[0][z[0]] = 0;
    for (int i = 0; i < r - 1; i++) {
      for (int j = 0; j < n; j++) {
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + l[z[i]][z[i + 1]]);
        for (int k = 0; k < n; k++)
          dp[i + 1][k] = min(dp[i + 1][k],
                             dp[i][j] + l[z[i]][j] + s[j][k] + l[k][z[i + 1]]);
      }
    }
    int ans = INF;
    for (int i = 0; i < n; i++)
      ans = min(ans, dp[r - 1][i]);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
struct st {
  int d, v, b, c;
  st() {}
  st(int d, int v, int b, int c) : d(d), v(v), b(b), c(c) {}
};
struct edge {
  int to, cost, s;
  edge() {}
  edge(int to, int cost, int s) : to(to), cost(cost), s(s) {}
};
int dp[2][222][222];
vector<edge> G[222];
signed main() {
  int n, m;
  while (cin >> n >> m, n) {
    for (int i = 0; i < 222; i++)
      G[i].clear();
    int x[m], y[m], t[m];
    char sl[m];
    for (int i = 0; i < m; i++)
      cin >> x[i] >> y[i] >> t[i] >> sl[i];
    int r;
    cin >> r;
    int z[r];
    for (int i = 0; i < r; i++)
      cin >> z[i];
    for (int i = 0; i < m; i++) {
      x[i]--;
      y[i]--;
      G[x[i]].emplace_back(y[i], t[i], sl[i] == 'S');
      G[y[i]].emplace_back(x[i], t[i], sl[i] == 'S');
    }
    for (int i = 0; i < r; i++)
      z[i]--;
    memset(dp, -1, sizeof(dp));
    deque<st> q;
    q.emplace_back(1, z[0], z[0], 0);
    dp[1][z[0]][z[0]] = 0;
    int pre = 1;
    int ans = 1LL << 55LL;
    while (!q.empty()) {
      st p = q.front();
      q.pop_front();
      int d = p.d, v = p.v, b = p.b, c = p.c;
      if (dp[d % 2][v][b] < c)
        continue;
      if (d == r) {
        ans = min(ans, dp[d % 2][v][b]);
        continue;
      }
      // cout<<d<<" "<<v<<" "<<b<<":"<<dp[d%2][v][b]<<endl;
      if (pre != p.d)
        for (int i = 0; i < n; i++)
          for (int j = 0; j < n; j++)
            dp[!(d % 2)][i][j] = -1;
      pre = d;
      for (edge &e : G[v]) {
        int nv = e.to, nb = b, nd = d;
        if (e.s) {
          if (v == b)
            nb = nv;
          else
            continue;
        }
        if (nv == z[d]) {
          nd++;
          if (dp[nd % 2][nv][nb] < 0 ||
              dp[nd % 2][nv][nb] > dp[d % 2][v][b] + e.cost) {
            // cout<<e.s<<":"<<nd<<" "<<nv<<" "<<nb<<endl;
            dp[nd % 2][nv][nb] = dp[d % 2][v][b] + e.cost;
            q.emplace_back(nd, nv, nb, dp[nd % 2][nv][nb]);
          }
        } else {
          if (dp[nd % 2][nv][nb] < 0 ||
              dp[nd % 2][nv][nb] > dp[d % 2][v][b] + e.cost) {
            dp[nd % 2][nv][nb] = dp[d % 2][v][b] + e.cost;
            q.emplace_front(nd, nv, nb, dp[nd % 2][nv][nb]);
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}