Problem p01317 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string.h>
#include <utility>
#include <vector>

using namespace std;

const int INF = 100000000;

int dS[201][201], dL[201][201], dp[1001][201], z[1001];

int main() {

  while (1) {

    int N, M;

    cin >> N >> M;

    if (N == 0)
      break;

    fill(dL[0], dL[201], INF);
    fill(dS[0], dS[201], INF);

    for (int i = 0; i < M; i++) {

      int s, t, v;
      char u;

      cin >> s >> t >> v >> u;

      if (u == 'L') {
        dL[s][t] = min(dL[s][t], v);
        dL[t][s] = min(dL[t][s], v);
      } else {
        dS[s][t] = min(dS[s][t], v);
        dS[t][s] = min(dS[t][s], v);
      }
    }

    int R;
    cin >> R;

    for (int i = 1; i <= R; i++) {
      cin >> z[i];
    }

    for (int k = 1; k <= N; k++) {
      for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= N; j++) {
          if (i == j) {
            dL[i][j] = 0;
            dS[i][j] = 0;
          }
          dL[i][j] = min(dL[i][j], dL[i][k] + dL[k][j]);
          dS[i][j] = min(dS[i][j], dS[i][k] + dS[k][j]);
        }
      }
    }

    fill(dp[0], dp[1001], INF);

    for (int i = 1; i <= N; i++) {
      dp[1][i] = dS[i][z[1]] + dL[z[1]][i];
    }

    for (int i = 2; i <= R; i++) {
      for (int j = 1; j <= N; j++) {
        for (int k = 1; k <= N; k++) {
          if (j != k)
            dp[i][k] = min(dp[i][k], dp[i - 1][j] + dL[z[i - 1]][j] + dS[j][k] +
                                         dL[k][z[i]]);
          else
            dp[i][j] = min(dp[i][j], dp[i - 1][j] + dL[z[i - 1]][z[i]]);
        }
      }
    }

    int ans = INF;

    for (int i = 1; i <= N; i++) {
      if (ans > dp[R][i])
        ans = dp[R][i];
    }

    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string.h>
#include <utility>
#include <vector>

using namespace std;

const int INF = 100000000;

int dS[201][201], dL[201][201], dp[1001][201], z[201];

int main() {

  while (1) {

    int N, M;

    cin >> N >> M;

    if (N == 0)
      break;

    fill(dL[0], dL[200], INF);
    fill(dS[0], dS[200], INF);

    for (int i = 0; i < M; i++) {

      int s, t, v;
      char u;

      cin >> s >> t >> v >> u;

      if (u == 'L') {
        dL[s][t] = v;
        dL[t][s] = v;
      } else {
        dS[s][t] = v;
        dS[t][s] = v;
      }
    }

    int R;
    cin >> R;

    for (int i = 1; i <= R; i++) {
      cin >> z[i];
    }

    for (int k = 1; k <= N; k++) {
      for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= N; j++) {
          dL[i][j] = min(dL[i][j], dL[i][k] + dL[k][j]);
          dS[i][j] = min(dS[i][j], dS[i][k] + dS[k][j]);
          if (i == j) {
            dL[i][j] = 0;
            dS[i][j] = 0;
          }
        }
      }
    }

    fill(dp[0], dp[200], INF);

    dp[1][1] = 0;

    for (int i = 2; i <= R; i++) {
      for (int j = 1; j <= N; j++) {
        for (int k = 1; k <= N; k++) {
          dp[i][k] = min(dp[i][k], dp[i - 1][j] + dL[z[i - 1]][j] + dS[j][k] +
                                       dL[k][z[i]]);
        }
      }
    }

    int ans = INF;

    for (int i = 1; i <= N; i++) {
      if (ans > dp[R][i])
        ans = dp[R][i];
    }

    cout << ans << endl;
  }
}