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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (long long i = 0; i < n; i++)
#define INF (1ll << 58)

int main() {
  long long n, m;
  while (cin >> n >> m && n) {
    long long land[200][200], ship[200][200];
    rep(i, 200) rep(j, 200) land[i][j] = ship[i][j] = INF;
    rep(i, 200) land[i][i] = ship[i][i] = 0;

    rep(i, m) {
      string l;
      long long a, b, c;
      cin >> a >> b >> c >> l;
      a--, b--;
      if (l == "L")
        land[a][b] = land[b][a] = min(c, land[a][b]);
      else
        ship[a][b] = ship[b][a] = min(c, ship[a][b]);
    }
    rep(k, n) rep(i, n) rep(j, n) land[i][j] =
        min(land[i][j], land[i][k] + land[k][j]);
    rep(k, n) rep(i, n) rep(j, n) ship[i][j] =
        min(ship[i][j], ship[i][k] + ship[k][j]);

    long long r;
    cin >> r;
    long long R[1000];
    rep(i, r) cin >> R[i], R[i]--;

    long long dp[2][200];
    rep(j, 2) rep(k, 200) dp[j][k] = INF;
    dp[0][R[0]] = 0;

    rep(bit, r - 1) {
      rep(cur, n) {
        rep(next, n) {
          long long newCost = dp[bit % 2][cur] + land[R[bit]][cur] +
                              ship[cur][next] + land[next][R[bit + 1]];
          dp[(bit + 1) % 2][next] = min(dp[(bit + 1) % 2][next], newCost);
        }
        dp[(bit + 1) % 2][cur] =
            min(dp[(bit + 1) % 2][cur],
                dp[bit % 2][cur] + land[R[bit]][R[bit + 1]]);
      }
      rep(j, 200) dp[bit % 2][j] = INF;
    }
    long long ret = INF;
    rep(i, n) { ret = min(dp[(r - 1) % 2][i], ret); }
    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000
class NODE {
public:
  int cost, pos, shipPos;
  NODE(int a, int b, int c) { cost = a, pos = b, shipPos = c; };
};
bool operator<(const NODE &a, const NODE &b) { return a.cost > b.cost; }
vector<vector<NODE>> land;
vector<vector<NODE>> ship;

int done[200][200];
void dijkstra(int s, int t, int z) {
  priority_queue<NODE> Q;
  Q.push(NODE(0, s, z));
  rep(i, 200) rep(j, 200) done[i][j] = INF;
  while (!Q.empty()) {
    NODE q = Q.top();
    Q.pop();
    // cout << q.pos << " " << q.shipPos << endl;
    if (done[q.pos][q.shipPos] > q.cost) {
      done[q.pos][q.shipPos] = q.cost;
    } else
      continue;

    if (q.pos == q.shipPos) {
      rep(i, ship[q.pos].size()) {
        Q.push(NODE(q.cost + ship[q.pos][i].cost, ship[q.pos][i].pos,
                    ship[q.pos][i].pos));
      }
    }
    rep(i, land[q.pos].size()) {
      Q.push(NODE(q.cost + land[q.pos][i].cost, land[q.pos][i].pos, q.shipPos));
    }
  }
}

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    // rep(i,200)rep(j,200)land[i][j] = ship[i][j] = INF;
    land.clear(), ship.clear();
    land.resize(n), ship.resize(n);
    rep(i, m) {
      string l;
      int a, b, c;
      cin >> a >> b >> c >> l;
      a--, b--;
      if (l == "L")
        land[a].push_back(NODE(c, b, 0)), land[b].push_back(NODE(c, a, 0));
      else
        ship[a].push_back(NODE(c, b, 0)), ship[b].push_back(NODE(c, a, 0));
    }
    int r;
    cin >> r;
    int R[1000];
    rep(i, r) { cin >> R[i], R[i]--; }
    int dp[2][200][200];
    rep(i, 2) rep(j, 200) rep(k, 200) dp[i][j][k] = INF;
    dp[0][R[0]][R[0]] = 0;
    rep(bit, r - 1) {
      rep(sp, 200) {
        if (dp[bit % 2][R[bit]][sp] != INF) {
          // cout << bit << " " << sp << " cost is " << dp[bit%2][R[bit]][sp] <<
          // "[[" << endl;
          dijkstra(R[bit], R[bit + 1], sp);
          rep(j, 200) dp[(bit + 1) % 2][R[bit + 1]][j] =
              min(dp[(bit + 1) % 2][R[bit + 1]][j],
                  dp[bit % 2][R[bit]][sp] + done[R[bit + 1]][j]);
        }
      }
      rep(i, 200) rep(j, 200) dp[bit % 2][i][j] = INF;
    }
    int ret = INF;
    rep(i, 200) { ret = min(dp[(r - 1) % 2][R[r - 1]][i], ret); }
    cout << ret << endl;
  }
}