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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 27;

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};

typedef vector<Weight> Array;
typedef vector<Array> Matrix;
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void shortestPath(const Matrix &g, Matrix &dist) {
  int n = g.size();
  dist = g;
  REP(k, n)
  REP(i, n) REP(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    Matrix gl(n, Array(n, INF));
    Matrix gs(n, Array(n, INF));
    set<int> ses;
    REP(i, n) { gl[i][i] = 0; }
    REP(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      char ch;
      cin >> ch;
      if (ch == 'L') {
        gl[a][b] = c;
        gl[b][a] = c;
      } else {
        gs[a][b] = c;
        gs[b][a] = c;
        ses.insert(a);
        ses.insert(b);
      }
    }
    FOR(it, ses)
    gs[*it][*it] = 0;
    int t;
    cin >> t;
    int z[t];
    REP(i, t) {
      cin >> z[i];
      z[i]--;
    }
    Matrix distl, dists;
    shortestPath(gl, distl);
    shortestPath(gs, dists);
    vector<int> ikerul[n], ikerus[n];
    REP(i, n) {
      FOR(jt, ses) {
        int j = *jt;
        if (dists[i][j] != INF)
          ikerus[i].push_back(j);
        if (distl[i][j] != INF)
          ikerul[i].push_back(j);
      }
    }

    int dp[t][n];
    REP(i, t) REP(j, n) dp[i][j] = INF;
    dp[0][z[0]] = 0;
    int cnt = 0;
    REP(i, t - 1) {
      REP(j, n) {
        REP(k, n) {
          int tmp;
          if (j == k)
            tmp = distl[z[i]][z[i + 1]];
          else
            tmp = distl[z[i]][j] + dists[j][k] + distl[k][z[i + 1]];
          dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + tmp);
        }
      }
    }
    cout << *min_element(dp[t - 1], dp[t - 1] + n) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};

typedef vector<Weight> Array;
typedef vector<Array> Matrix;
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void shortestPath(const Matrix &g, Matrix &dist) {
  int n = g.size();
  dist = g;
  REP(k, n)
  REP(i, n) REP(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    Matrix gl(n, Array(n, INF));
    Matrix gs(n, Array(n, INF));
    set<int> ses;
    REP(i, n) { gl[i][i] = 0; }
    REP(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      char ch;
      cin >> ch;
      if (ch == 'L') {
        gl[a][b] = c;
        gl[b][a] = c;
      } else {
        gs[a][b] = c;
        gs[b][a] = c;
        ses.insert(a);
        ses.insert(b);
      }
    }
    FOR(it, ses)
    gs[*it][*it] = 0;
    int t;
    cin >> t;
    int z[t];
    REP(i, t) {
      cin >> z[i];
      z[i]--;
    }
    Matrix distl, dists;
    shortestPath(gl, distl);
    shortestPath(gs, dists);
    vector<int> ikerul[n], ikerus[n];
    REP(i, n) {
      REP(j, n) {
        if (dists[i][j] != INF)
          ikerus[i].push_back(j);
        if (distl[i][j] != INF)
          ikerul[i].push_back(j);
      }
    }

    int dp[t][n];
    REP(i, t) REP(j, n) dp[i][j] = INF;
    dp[0][z[0]] = 0;
    REP(i, t - 1) {
      FOR(it, ikerul[z[i]]) {
        FOR(jt, ikerus[*it]) {
          FOR(kt, ikerul[*jt]) {
            if (*kt == z[i + 1]) {
              dp[i + 1][*jt] =
                  min(dp[i + 1][*jt], dp[i][*it] + distl[z[i]][*it] +
                                          dists[*it][*jt] + distl[*jt][*kt]);
              // printf("%d, %d->%d->%d->%d\\n", i,z[i],*it,*jt,*kt);
              // cout << dp[i+1][*jt] << endl;
            }
          }
        }
      }
    }
    cout << *min_element(dp[t - 1], dp[t - 1] + n) << endl;
  }
}