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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <vector>

#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)

using namespace std;

const int INF = 1 << 28;

int main() {

  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

    vector<vector<int>> L(n + 1, vector<int>(n + 1, INF));
    vector<vector<int>> S(n + 1, vector<int>(n + 1, INF));

    int from, to, dist, r;
    string sl;

    rep(i, m) {
      cin >> from >> to >> dist >> sl;
      if (sl == "L") {
        L[from][to] = dist;
        L[to][from] = dist;
      } else {
        S[from][to] = dist;
        S[to][from] = dist;
      }
    }

    rep(i, n + 1) {
      L[i][i] = 0;
      S[i][i] = 0;
    }

    cin >> r;

    vector<int> z(r + 1);
    rep(i, r) cin >> z[i];

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

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

    int dp[r][n + 1];
    fill_n((int *)dp, sizeof(dp) / sizeof(int), INF);
    dp[0][z[0]] = 0;
    for (int i = 1; i < r; i++) {
      //????????????
      for (int j = 1; j <= n; j++) {
        if (dp[i - 1][j] != INF) {
          dp[i][j] = dp[i - 1][j] + L[z[i - 1]][z[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] + L[z[i - 1]][j] + S[j][k] + L[k][z[i]]);
        }
      }
    }

    cout << *min_element(dp[r - 1], dp[r - 1] + n + 1) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <vector>

#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)

using namespace std;

const int INF = 1 << 28;

int main() {

  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<vector<int>> L(n + 1, vector<int>(n + 1, INF));
    vector<vector<int>> S(n + 1, vector<int>(n + 1, INF));

    int from, to, dist, r;
    string sl;
    vector<int> z(r + 1);

    rep(i, m) {
      cin >> from >> to >> dist >> sl;
      if (sl == "L") {
        L[from][to] = dist;
        L[to][from] = dist;
      } else {
        S[from][to] = dist;
        S[to][from] = dist;
      }
    }

    rep(i, n + 1) {
      L[i][i] = 0;
      S[i][i] = 0;
    }

    cin >> r;
    rep(i, r) cin >> z[i];

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

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

    int dp[r][n + 1];
    fill_n((int *)dp, sizeof(dp) / sizeof(int), INF);
    dp[0][z[0]] = 0;
    for (int i = 1; i < r; i++) {
      //????????????
      for (int j = 1; j <= n; j++) {
        if (dp[i - 1][j] != INF) {
          dp[i][j] = dp[i - 1][j] + L[z[i - 1]][z[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] + L[z[i - 1]][j] + S[j][k] + L[k][z[i]]);
        }
      }
    }

    cout << *min_element(dp[r - 1], dp[r - 1] + n + 1) << endl;
  }
  return 0;
}