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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n, m, r;
const int INF = 1e7;
int land[200][200], sea[200][200], z[1000];
typedef pair<int, int> P;

int main() {

  while (cin >> n >> m, n) {

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        land[i][j] = sea[i][j] = INF;
      land[i][i] = sea[i][i] = 0;
    }

    for (int i = 0; i < m; i++) {
      int x, y, t;
      string s;
      cin >> x >> y >> t >> s;
      x--;
      y--;
      if (s == "S") {
        sea[x][y] = min(sea[x][y], t);
        sea[y][x] = min(sea[y][x], t);
      } else {
        land[x][y] = min(land[x][y], t);
        land[y][x] = min(land[y][x], t);
      }
    }

    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          sea[i][j] = min(sea[i][j], sea[i][k] + sea[k][j]);
          land[i][j] = min(land[i][j], land[i][k] + land[k][j]);
        }
      }
    }

    cin >> r;
    for (int i = 0; i < r; i++) {
      cin >> z[i];
      z[i]--;
    }

    vector<vector<int>> dp(1001, vector<int>(201, INF));
    dp[0][z[0]] = 0;

    for (int i = 1; i < r; i++) {
      for (int j = 0; j < n; j++) {

        // z[i]???????????????????????¨??°???z[i-1]

        for (int k = 0; k < n; k++) {
          //????????????j?????§?§???????j??????k????????????k??????z[i]?????????
          dp[i][k] = min(dp[i][k], dp[i - 1][j] + land[z[i - 1]][j] +
                                       sea[j][k] + land[k][z[i]]);
        }

        //????????§?§????
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + land[z[i - 1]][z[i]]);
      }
    }

    int res = INF;
    for (int i = 0; i < n; i++) {
      res = min(res, dp[r - 1][i]);
    }

    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n, m, r;
const int INF = 1e7;
int land[200][200], sea[200][200], z[1000];
typedef pair<int, int> P;

int main() {

  while (cin >> n >> m, n) {

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        land[i][j] = sea[i][j] = INF;
      land[i][i] = sea[i][i] = 0;
    }

    for (int i = 0; i < m; i++) {
      int x, y, t;
      string s;
      cin >> x >> y >> t >> s;
      x--;
      y--;
      if (s == "S") {
        sea[x][y] = min(sea[x][y], t);
        sea[y][x] = min(sea[y][x], t);
      } else {
        land[x][y] = min(land[x][y], t);
        land[y][x] = min(land[y][x], t);
      }
    }

    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          sea[i][j] = min(sea[i][j], sea[i][k] + sea[k][j]);
          land[i][j] = min(land[i][j], land[i][k] + land[k][j]);
        }
      }
    }

    cin >> r;
    for (int i = 0; i < r; i++) {
      cin >> z[i];
      z[i]--;
    }

    // dp[i][j] 0~i?????§???????????????????????????j???????°???????
    vector<vector<int>> dp(2001, vector<int>(201, INF));
    dp[0][z[0]] = 0;

    stack<P> st;
    st.push(P(0, z[0]));
    int res = INF;
    while (!st.empty()) {

      P p = st.top();
      st.pop();
      int idx = p.first, ship = p.second;

      // cout<<"idx="<<idx<<" ship="<<ship<<" cost="<<dp[idx][ship]<<endl;

      if (idx == r - 1) {
        continue;
      }

      int nidx = idx + 1;

      //????????§?§????
      if (land[z[idx]][z[nidx]] != INF) {
        if (dp[nidx][ship] > dp[idx][ship] + land[z[idx]][z[nidx]]) {
          dp[nidx][ship] = dp[idx][ship] + land[z[idx]][z[nidx]];
          st.push(P(nidx, ship));
        }
      }

      // v?????§?????§?§????????????????????????§?§????
      for (int v = 0; v < n; v++) {
        if (sea[ship][v] == INF || land[v][z[nidx]] == INF)
          continue;

        if (dp[nidx][v] > dp[idx][ship] + sea[ship][v] + land[v][z[nidx]]) {
          dp[nidx][v] = dp[idx][ship] + sea[ship][v] + land[v][z[nidx]];
          st.push(P(nidx, v));
        }
      }
    }

    for (int i = 0; i < n; i++) {
      res = min(res, dp[r - 1][i]);
    }

    cout << res << endl;
  }
}