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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<ll, P> State;

const ll INF = 1001001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

struct Edge {
  int to, cost;
};

ll dl[200][200], ds[200][200], dp[1000][200];
int N, M, R;
int Z[1000];

int main() {
  while (std::cin >> N >> M, N) {

    REP(i, 200) {
      REP(j, 200) {
        if (i == j) {
          dl[i][j] = 0;
          ds[i][j] = 0;
        } else {
          dl[i][j] = INF;
          ds[i][j] = INF;
        }
      }
    }

    REP(i, M) {
      int from, to, cost;
      char c;
      std::cin >> from >> to >> cost >> c;
      from--;
      to--;

      if (c == 'L') {
        dl[from][to] = std::min(dl[from][to], static_cast<ll>(cost));
        dl[to][from] = std::min(dl[to][from], static_cast<ll>(cost));
      } else if (c == 'S') {
        ds[from][to] = std::min(ds[from][to], static_cast<ll>(cost));
        ds[to][from] = std::min(ds[to][from], static_cast<ll>(cost));
      }
    }

    REP(k, N) {
      REP(i, N) {
        REP(j, N) {
          dl[i][j] = std::min(dl[i][j], dl[i][k] + dl[k][j]);
          ds[i][j] = std::min(ds[i][j], ds[i][k] + ds[k][j]);
        }
      }
    }

    std::cin >> R;
    REP(i, R) {
      std::cin >> Z[i];
      Z[i]--;
    }

    REP(i, 1000) {
      REP(j, 200) { dp[i][j] = INF; }
    }
    dp[0][Z[0]] = 0;

    REP(i, R - 1) {
      REP(j, N) {
        // A->陸->C->海->D->陸->B
        REP(k, N) {
          dp[i + 1][j] = std::min(dp[i + 1][j], dp[i][k] + dl[Z[i]][k] +
                                                    ds[k][j] + dl[j][Z[i + 1]]);
        }
        // A->陸->B
        dp[i + 1][j] = std::min(dp[i + 1][j], dp[i][j] + dl[Z[i]][Z[i + 1]]);
      }
    }

    ll res = INF;
    REP(i, N) { res = std::min(res, dp[R - 1][i]); }

    std::cout << res << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<ll, P> State;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

struct Edge {
  int to, cost;
};

int dl[200][200], ds[200][200];
int N, M, R;
int Z[1000];
std::vector<Edge> Gl[200], Gs[200];

int dijkstra() {
  int d[1000][200];
  REP(i, 1000) {
    REP(j, 200) { d[i][j] = INF; }
  }

  // State: (コスト, (今いるところ, 船を置いたところ))
  std::priority_queue<State> q;
  d[0][0] = 0;
  q.push(mp(0, mp(0, 0)));

  while (!q.empty()) {
    State s = q.top();
    q.pop();
    int cost = s.first;
    P p = s.second;

    if (p.first == R - 1) {
      continue;
    }
    if (d[p.first][p.second] < cost) {
      continue;
    }

    // A->陸->B
    if (d[p.first + 1][p.second] > cost + dl[Z[p.first]][Z[p.first + 1]]) {
      d[p.first + 1][p.second] = cost + dl[Z[p.first]][Z[p.first + 1]];
      q.push(mp(d[p.first + 1][p.second], mp(p.first + 1, p.second)));
    }

    // A->陸->C->海->D->陸->B
    for (auto &e : Gs[p.second]) {
      if (d[p.first + 1][e.to] >
          cost + dl[Z[p.first]][p.second] + e.cost + dl[e.to][Z[p.first + 1]]) {
        d[p.first + 1][e.to] =
            cost + dl[Z[p.first]][p.second] + e.cost + dl[e.to][Z[p.first + 1]];
        q.push(mp(d[p.first + 1][e.to], mp(p.first + 1, e.to)));
      }
    }
  }

  int res = INF;
  REP(i, N) { res = std::min(res, d[R - 1][i]); }

  return res;
}

int main() {
  while (std::cin >> N >> M, N) {
    REP(i, 200) {
      REP(j, 200) {
        if (i == j) {
          dl[i][j] = 0;
          ds[i][j] = 0;
        } else {
          dl[i][j] = INF;
          ds[i][j] = INF;
        }
      }
    }

    REP(i, 200) {
      Gl[i].clear();
      Gs[i].clear();
    }

    REP(i, M) {
      int from, to, cost;
      char c;
      std::cin >> from >> to >> cost >> c;
      from--;
      to--;

      if (c == 'L') {
        dl[from][to] = std::min(dl[from][to], cost);
        dl[to][from] = std::min(dl[to][from], cost);
      } else if (c == 'S') {
        ds[from][to] = std::min(ds[from][to], cost);
        ds[to][from] = std::min(ds[to][from], cost);
      }
    }

    std::cin >> R;
    REP(i, R) {
      std::cin >> Z[i];
      Z[i]--;
    }

    REP(k, N) {
      REP(i, N) {
        REP(j, N) {
          dl[i][j] = std::min(dl[i][j], dl[i][k] + dl[k][j]);
          ds[i][j] = std::min(ds[i][j], ds[i][k] + ds[k][j]);
        }
      }
    }

    REP(i, N) {
      REP(j, N) {
        if (dl[i][j] < INF) {
          Gl[i].push_back({j, dl[i][j]});
          Gl[j].push_back({i, dl[i][j]});
        }
        if (ds[i][j] < INF) {
          Gs[i].push_back({j, ds[i][j]});
          Gs[j].push_back({i, ds[i][j]});
        }
      }
    }

    std::cout << dijkstra() << std::endl;
  }
}