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

#include <algorithm>
#include <iostream>

using namespace std;

#define NMAX 200
#define RMAX 1000
#define INF 100000000

typedef long long ll;

int main(void) {

  ll N, M;
  ll x, y, t;
  char sl;
  ll R, zi;
  ll bzi;
  ll ans;
  ll ldp[NMAX + 2][NMAX + 2];
  ll sdp[NMAX + 2][NMAX + 2];
  ll dp[NMAX + 2][RMAX + 2];

  while (1) {

    cin >> N >> M;
    if (N == 0 && M == 0)
      break;

    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= N; j++) {
        ldp[i][j] = INF;
        sdp[i][j] = INF;
      }
      ldp[i][i] = 0;
      sdp[i][i] = 0;
    }

    for (int i = 0; i < M; i++) {
      cin >> x >> y >> t >> sl;
      if (sl == 'L') {
        ldp[x][y] = min(ldp[x][y], t);
        ldp[y][x] = min(ldp[y][x], t);
      } else {
        sdp[x][y] = min(sdp[x][y], t);
        sdp[y][x] = min(sdp[y][x], t);
      }
    }

    for (int k = 1; k <= N; k++) {
      for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= N; j++) {
          ldp[i][j] = min(ldp[i][j], ldp[i][k] + ldp[k][j]);
          sdp[i][j] = min(sdp[i][j], sdp[i][k] + sdp[k][j]);
        }
      }
    }

    cin >> R;

    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= R; j++) {
        dp[i][j] = INF;
      }
    }

    cin >> bzi;

    for (int i = 1; i <= N; i++) {
      dp[i][0] = sdp[bzi][i] + ldp[i][bzi];
    }

    for (int i = 1; i < R; i++) {
      cin >> zi;
      for (int j = 1; j <= N; j++) {
        for (int k = 1; k <= N; k++) {
          // cout<<i<<","<<j<<","<<k<<","<<dp[j][i]<<","<<ldp[j][zi]<<","<<sdp[k][j]<<","<<ldp[bzi][k]<<","<<dp[k][i-1]<<","<<ldp[zi][bzi]<<endl;
          if (j != k)
            dp[j][i] = min(dp[j][i],
                           ldp[j][zi] + sdp[k][j] + ldp[bzi][k] + dp[k][i - 1]);
          else
            dp[j][i] = min(dp[j][i], dp[k][i - 1] + ldp[bzi][zi]);
        }
      }
      bzi = zi;
    }

    ans = dp[1][R - 1];
    for (int i = 2; i <= N; i++) {
      ans = min(ans, dp[i][R - 1]);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

#define NMAX 200
#define RMAX 1000
#define INF 100000000

typedef long long ll;

int main(void) {

  ll N, M;
  ll x, y, t;
  char sl;
  ll R, zi;
  ll bzi;
  ll ans;
  ll ldp[NMAX + 2][NMAX + 2];
  ll sdp[NMAX + 2][NMAX + 2];
  ll dp[NMAX + 2][RMAX + 2];

  while (1) {

    cin >> N >> M;
    if (N == 0 && M == 0)
      break;

    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= N; j++) {
        ldp[i][j] = INF;
        sdp[i][j] = INF;
      }
      ldp[i][i] = 0;
      sdp[i][i] = 0;
    }

    for (int i = 0; i < M; i++) {
      cin >> x >> y >> t >> sl;
      if (sl == 'L') {
        ldp[x][y] = min(ldp[x][y], t);
        ldp[y][x] = min(ldp[y][x], t);
      } else {
        sdp[x][y] = min(sdp[x][y], t);
        sdp[y][x] = min(sdp[y][x], t);
      }
    }

    for (int k = 1; k <= N; k++) {
      for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= N; j++) {
          ldp[i][j] = min(ldp[i][j], ldp[i][k] + ldp[k][j]);
          sdp[i][j] = min(sdp[i][j], sdp[i][k] + sdp[k][j]);
        }
      }
    }

    cin >> R;

    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= R; j++) {
        dp[i][j] = INF;
      }
    }
    dp[1][0] = 0;

    bzi = 1;

    for (int i = 1; i <= R; i++) {
      cin >> zi;
      for (int j = 1; j <= N; j++) {
        for (int k = 1; k <= N; k++) {
          cout << i << "," << j << "," << k << "," << dp[j][i] << ","
               << ldp[j][zi] << "," << sdp[k][j] << "," << ldp[bzi][k] << ","
               << dp[k][i - 1] << endl;
          if (j == k)
            dp[j][i] = min(dp[j][i],
                           ldp[j][zi] + sdp[k][j] + ldp[bzi][k] + dp[k][i - 1]);
          else
            dp[j][i] = min(dp[j][i], dp[k][i - 1] + ldp[bzi][zi]);
        }
      }
      bzi = zi;
    }

    ans = dp[1][R];
    for (int i = 2; i <= N; i++) {
      ans = min(ans, dp[i][R]);
    }
    cout << ans << endl;
  }
}