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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

const int INF = 10000000;
int ans;
int n, m;
int r;
int load[201][201];
int sea[201][201];
int dp[1001][201];
int rt[1001];
int main(void) {
  while (1) {
    for (int i = 0; i <= 200; i++) {
      for (int j = 0; j <= 200; j++) {
        load[i][j] = sea[i][j] = INF;
      }
      load[i][i] = sea[i][i] = 0;
      for (int j = 0; j <= 1000; j++) {
        dp[j][i] = INF;
      }
    }
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < m; i++) {
      int a, b, ti;
      string ls;
      cin >> a >> b >> ti >> ls;
      a--, b--;
      if (ls == "L") {
        load[a][b] = min(load[a][b], ti);
        load[b][a] = min(load[b][a], ti);
      }
      if (ls == "S") {
        sea[a][b] = min(sea[a][b], ti);
        sea[b][a] = min(sea[b][a], ti);
      }
    }
    for (int k = 0; k <= 200; k++) {
      for (int i = 0; i <= 200; i++) {
        for (int j = 0; j <= 200; j++) {
          load[i][j] = min(load[i][j], load[i][k] + load[k][j]);
          sea[i][j] = min(sea[i][j], sea[i][k] + sea[k][j]);
        }
      }
    }
    cin >> r;
    for (int i = 0; i < r; i++) {
      cin >> rt[i];
      rt[i]--;
    }
    dp[0][rt[0]] = 0;
    // 配達先
    for (int i = 0; i < r - 1; i++) {
      // 次の船の位置
      for (int j = 0; j <= 200; j++) {
        // 今の船の位置
        for (int k = 0; k <= 200; k++) {
          if (dp[i][k] != INF) {
            dp[i + 1][j] =
                min(dp[i + 1][j],
                    dp[i][k] + load[rt[i]][k] + sea[k][j] + load[j][rt[i + 1]]);
            if (j == k)
              dp[i + 1][j] =
                  min(dp[i + 1][j], dp[i][k] + load[rt[i]][rt[i + 1]]);
          }
        }
      }
    }
    int res = INF;
    for (int i = 0; i <= 200; i++)
      res = min(res, dp[r - 1][i]);
    printf("%d\\n", res);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

struct edge {
  int t, c;
  edge(int t, int c) : t(t), c(c) {}
};
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int INF = 1000000000;
int ans;
int n, m;
int r;
int cost[200][200][2];
vector<edge> l[200];
vector<edge> s[200];
int rt[1001];
bool reset[1001];

int dijk() {
  for (int i = 0; i < r - 1; i++) {
    priority_queue<PP, vector<PP>, greater<PP>> que;
    if (i == 0)
      que.push(PP(0, P(rt[i], rt[i])));
    else {
      for (int j = 0; j < n; j++) {
        que.push(PP(cost[rt[i]][j][1], P(rt[i], j)));
      }
    }
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        cost[j][k][0] = cost[j][k][1] = INF;
      }
    }
    while (que.size()) {
      PP p = que.top();
      que.pop();
      int c = p.first, v = p.second.first, sh = p.second.second;
      if (c >= INF)
        continue;
      if (c > cost[v][sh][0])
        continue;
      if (v == rt[i + 1])
        cost[v][sh][1] = c;
      if (i == r - 2 && v == rt[i + 1])
        return c;
      for (int i = 0; i < l[v].size(); i++) {
        edge e = l[v][i];
        if (c + e.c < cost[e.t][sh][0]) {
          cost[e.t][sh][0] = c + e.c;
          que.push(PP(c + e.c, P(e.t, sh)));
        }
      }
      if (sh == v)
        for (int i = 0; i < s[v].size(); i++) {
          edge e = s[v][i];
          if (c + e.c < cost[e.t][e.t][0]) {
            cost[e.t][e.t][0] = c + e.c;
            que.push(PP(c + e.c, P(e.t, e.t)));
          }
        }
    }
  }
}

int main(void) {
  while (1) {
    for (int i = 0; i < 200; i++) {
      l[i].clear();
      s[i].clear();
    }
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < m; i++) {
      int a, b, ti;
      string ls;
      cin >> a >> b >> ti >> ls;
      if (ls == "L") {
        l[a - 1].push_back(edge(b - 1, ti));
        l[b - 1].push_back(edge(a - 1, ti));
      }
      if (ls == "S") {
        s[a - 1].push_back(edge(b - 1, ti));
        s[b - 1].push_back(edge(a - 1, ti));
      }
    }
    cin >> r;
    for (int i = 0; i < r; i++) {
      cin >> rt[i];
      rt[i]--;
    }
    cout << dijk() << endl;
  }
  return 0;
}