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

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N, M;
  while (cin >> N >> M, N) {
    vector<vector<long long>> L(N, vector<long long>(N));
    vector<vector<long long>> S(N, vector<long long>(N));
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        if (i == j) {
          L[i][j] = 0;
          S[i][j] = 0;
        } else {
          L[i][j] = 1e8;
          S[i][j] = 1e8;
        }
      }
    }
    for (int i = 0; i < M; ++i) {
      int x, y, t;
      char sl;
      cin >> x >> y >> t >> sl;
      --x;
      --y;
      if (sl == 'L') {
        L[x][y] = t;
        L[y][x] = t;
      } else {
        S[x][y] = t;
        S[y][x] = t;
      }
    }
    int R;
    cin >> R;
    vector<int> z(R);
    for (int i = 0; i < R; ++i) {
      int a;
      cin >> a;
      z[i] = --a;
    }
    for (int k = 0; k < N; ++k) {
      for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
          L[i][j] = min(L[i][j], L[i][k] + L[k][j]);
          S[i][j] = min(S[i][j], S[i][k] + S[k][j]);
        }
      }
    }
    vector<vector<long long>> dp(R, vector<long long>(N, 1e10));
    dp[0][z[0]] = 0;
    long long ans = 1e18;
    for (int i = 1; i < R; ++i) {
      for (int j = 0; j < N; ++j) {
        dp[i][j] = dp[i - 1][j] + L[z[i - 1]][z[i]];
        for (int k = 0; k < N; ++k) {
          dp[i][j] = min(dp[i][j],
                         dp[i - 1][k] + L[z[i - 1]][k] + S[k][j] + L[j][z[i]]);
        }
        if (i == R - 1) {
          ans = min(ans, dp[i][j]);
        }
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N, M;
  while (cin >> N >> M, N) {
    vector<vector<int>> L(N, vector<int>(N));
    vector<vector<int>> S(N, vector<int>(N));
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        if (i == j) {
          L[i][j] = 0;
          S[i][j] = 0;
        } else {
          L[i][j] = 1e8;
          S[i][j] = 1e8;
        }
      }
    }
    for (int i = 0; i < M; ++i) {
      int x, y, t;
      char sl;
      cin >> x >> y >> t >> sl;
      --x;
      --y;
      if (sl == 'L') {
        L[x][y] = t;
        L[y][x] = t;
      } else {
        S[x][y] = t;
        S[y][x] = t;
      }
    }
    int R;
    cin >> R;
    vector<int> z(R);
    for (int i = 0; i < R; ++i) {
      int a;
      cin >> a;
      z[i] = --a;
    }
    for (int k = 0; k < N; ++k) {
      for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
          L[i][j] = min(L[i][j], L[i][k] + L[k][j]);
          S[i][j] = min(S[i][j], S[i][k] + S[k][j]);
        }
      }
    }
    vector<vector<long long>> dp(N, vector<long long>(R, 1e10));
    dp[0][z[0]] = 0;
    long long ans = 1e18;
    for (int i = 1; i < R; ++i) {
      for (int j = 0; j < N; ++j) {
        dp[i][j] = dp[i - 1][j] + L[z[i - 1]][z[i]];
        for (int k = 0; k < N; ++k) {
          dp[i][j] = min(dp[i][j],
                         dp[i - 1][k] + L[z[i - 1]][k] + S[k][j] + L[j][z[i]]);
        }
        if (i == R - 1) {
          ans = min(ans, dp[i][j]);
        }
      }
    }
    cout << ans << endl;
  }
}