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

#include <algorithm>
#include <cstdio>

#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

int n, m, r;
long long sd[200][200];
long long ld[200][200];
int z[1000];
long long dp[200][1000];

int main() {
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0) {
      break;
    }

    rep(i, n) {
      fill(sd[i], sd[i] + n, 2e10);
      fill(ld[i], ld[i] + n, 2e10);
      sd[i][i] = 0;
      ld[i][i] = 0;
    }

    rep(i, m) {
      int x, y, t;
      char c;
      scanf("%d%d%d %c", &x, &y, &t, &c);
      --x;
      --y;
      if (c == 'S') {
        sd[x][y] = sd[y][x] = t;
      } else {
        ld[x][y] = ld[y][x] = 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]);
        }
      }
    }

    scanf("%d", &r);
    rep(i, r) {
      scanf("%d", z + i);
      --z[i];
    }

    rep(j, r) {
      rep(i, n) {
        if (j == 0) {
          dp[i][j] = (i == z[j] ? 0 : 2e10);
        } else {
          dp[i][j] = dp[i][j - 1] + ld[z[j - 1]][z[j]];
          rep(k, n) {
            dp[i][j] = min(dp[i][j], dp[k][j - 1] + ld[z[j - 1]][k] + sd[k][i] +
                                         ld[i][z[j]]);
          }
        }
      }
    }

    long long result = 2e10;
    rep(i, n) { result = min(result, dp[i][r - 1]); }

    printf("%lld\\n", result);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>

#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<long long, int> P;

struct E {
  int t, c;
};

int n, m, r;
vector<E> g[40000];
long long d[40000];

int main() {
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0) {
      break;
    }

    rep(i, m) {
      int x, y, t;
      char c;
      scanf("%d%d%d %c", &x, &y, &t, &c);
      --x;
      --y;
      if (c == 'L') {
        rep(j, n) {
          g[x * n + j].push_back(E{y * n + j, t});
          g[y * n + j].push_back(E{x * n + j, t});
        }
      } else {
        g[x * n + x].push_back(E{y * n + y, t});
        g[y * n + y].push_back(E{x * n + x, t});
      }
    }
    scanf("%d", &r);

    rep(i, r - 1) {
      int z;
      scanf("%d", &z);
      --z;

      priority_queue<P, vector<P>, greater<P>> q;
      if (i == 0) {
        fill(d, d + n * n, 3e12);
        d[z * n + z] = 0;
        q.push({0, z * n + z});
      } else {
        rep(j, n) {
          rep(k, n) {
            if (j == z) {
              q.push({d[j * n + k], j * n + k});
            } else {
              d[j * n + k] = 3e12;
            }
          }
        }
      }
      while (!q.empty()) {
        auto v = q.top();
        q.pop();
        if (v.first > d[v.second]) {
          continue;
        }
        for (auto &x : g[v.second]) {
          if (d[x.t] > d[v.second] + x.c) {
            d[x.t] = d[v.second] + x.c;
            q.push({d[x.t], x.t});
          }
        }
      }
    }

    int z;
    scanf("%d", &z);
    --z;
    printf("%lld\\n", *min_element(d + z * n, d + z * n + n));

    rep(i, n * n) { g[i].clear(); }
  }
  return 0;
}