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

#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define all(i) i.begin(), i.end()
#define rep(i, n) for (int i = 0; i < n; i++)

#define int long long
typedef pair<int, int> p;
const int INF = 1e16;

int dp[1001][201];

int ld[201][201];
int sd[201][201];

signed main() {
  int n, m;
  while (cin >> n >> m && n) {
    rep(i, 201) rep(j, 201) { ld[i][j] = sd[i][j] = INF; }
    rep(i, 201) {
      ld[i][i] = 0;
      sd[i][i] = 0;
    }
    rep(i, m) {
      int x, y, t;
      char s;
      cin >> x >> y >> t >> s;
      x--;
      y--;
      if (s == 'S') {
        sd[y][x] = sd[x][y] = t;
      } else {
        ld[y][x] = ld[x][y] = t;
      }
    }
    rep(k, n) rep(i, n) rep(j, n) {
      sd[i][j] = min(sd[i][j], sd[i][k] + sd[k][j]);
      ld[i][j] = min(ld[i][j], ld[i][k] + ld[k][j]);
    }
    int r;
    cin >> r;
    vector<int> z(r);
    rep(i, r) {
      cin >> z[i];
      z[i]--;
    }
    rep(i, 1001) rep(j, 201) { dp[i][j] = INF; }

    dp[0][z[0]] = 0;
    rep(i, r - 1) {
      rep(s, n) {
        // z[i] to z[i+1]
        dp[i + 1][s] = min(dp[i + 1][s], dp[i][s] + ld[z[i]][z[i + 1]]);
        rep(j, n) {
          dp[i + 1][j] = min(dp[i + 1][j], dp[i][s] + ld[z[i]][s] + sd[s][j] +
                                               ld[j][z[i + 1]]);
        }
      }
    }
    int ans = INF;
    rep(s, n) { ans = min(ans, dp[r - 1][s]); }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define all(i) i.begin(), i.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define mclr(n) memset(n, -1, sizeof(n))

#define int long long
typedef pair<int, int> p;
const int INF = 1e16;

int dp[1001][201];

int ld[201][201];
int sd[201][201];

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int n, m;
  while (cin >> n >> m && n) {
    rep(i, 201) rep(j, 201) { ld[i][j] = sd[i][j] = INF; }
    rep(i, 1001) rep(j, 201) { dp[i][j] = INF; }
    rep(i, m) {
      int x, y, t;
      char s;
      cin >> x >> y >> t >> s;
      x--;
      y--;
      if (s == 'S') {
        sd[y][x] = sd[x][y] = min(sd[x][y], t);
      } else {
        ld[y][x] = ld[x][y] = min(ld[x][y], t);
      }
    }
    int r;
    cin >> r;
    vector<int> z(r);
    rep(i, r) {
      cin >> z[i];
      z[i]--;
    }

    dp[0][z[0]] = 0;
    rep(i, r - 1) {
      rep(s, n) {
        // z[i]テ」ツ?凝」ツつ越[i+1]テ」ツ?ク
        // ティツ按ケテ」ツ?ョテ、ツスツ催ァツスツョテ」ツ?茎
        typedef tuple<int, int, int> p;
        static int me[201][201];
        mclr(me);
        priority_queue<p, vector<p>, greater<p>> q;
        q.push(p(dp[i][s], z[i], s));
        me[z[i]][s] = dp[i][s];
        while (q.size()) {
          int cost = get<0>(q.top());
          int nw = get<1>(q.top());
          int sh = get<2>(q.top());
          q.pop();
          if (me[nw][sh] < cost)
            continue;
          rep(j, n) {
            if (me[j][sh] == -1 || me[j][sh] > ld[nw][j] + me[nw][sh]) {
              me[j][sh] = ld[nw][j] + me[nw][sh];
              q.push(p(me[j][sh], j, sh));
            }
            if (sh == nw) {
              if (me[j][j] == -1 || me[j][j] > sd[nw][j] + me[nw][sh]) {
                me[j][j] = sd[nw][j] + me[nw][sh];
                q.push(p(me[j][j], j, j));
              }
            }
          }
        }
        rep(sh, n) { dp[i + 1][sh] = min(dp[i + 1][sh], me[z[i + 1]][sh]); }
      }
    }
    int ans = INF;
    rep(s, n) { ans = min(ans, dp[r - 1][s]); }
    cout << ans << endl;
  }

  return 0;
}