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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;

// →↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};

const int INF = 1 << 27;

int main() {
  while (true) {
    int N, M;
    cin >> N >> M;
    if (N == 0 and M == 0)
      break;
    vector<vector<vector<int>>> dist(
        2, vector<vector<int>>(N, vector<int>(N, INF)));
    for (int i = 0; i < M; i++) {
      int x, y, t;
      char sl;
      cin >> x >> y >> t >> sl;
      x--;
      y--;
      int isl = (sl == 'L') ? 0 : 1;
      dist[isl][x][y] = min(dist[isl][x][y], t);
      dist[isl][y][x] = min(dist[isl][y][x], t);
    }
    for (int i = 0; i < N; i++) {
      dist[0][i][i] = dist[1][i][i] = 0;
    }
    for (int k = 0; k < N; k++) {
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          dist[0][i][j] = min(dist[0][i][j], dist[0][i][k] + dist[0][k][j]);
          dist[1][i][j] = min(dist[1][i][j], dist[1][i][k] + dist[1][k][j]);
        }
      }
    }
    int R;
    cin >> R;
    vector<int> togo(R);
    for (int i = 0; i < R; i++) {
      cin >> togo[i];
      togo[i]--;
    }
    vector<vector<int>> dp(R, vector<int>(N, INF));
    dp[0][togo[0]] = 0;
    for (int i = 0; i < R - 1; i++) {
      for (int j = 0; j < N; j++) {
        if (dp[i][j] == INF)
          continue;
        for (int k = 0; k < N; k++) {
          dp[i + 1][k] =
              min(dp[i + 1][k], dp[i][j] + dist[0][togo[i]][j] + dist[1][j][k] +
                                    dist[0][k][togo[i + 1]]);
        }
        dp[i + 1][j] =
            min(dp[i + 1][j], dp[i][j] + dist[0][togo[i]][togo[i + 1]]);
      }
    }
    int ret = INF;
    for (int i = 0; i < N; i++) {
      ret = min(ret, dp[R - 1][i]);
    }
    cout << ret << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;

// →↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};

const double EPS = 1e-9;
const int INF = 1 << 20;

int main() {
  while (true) {
    int N, M;
    cin >> N >> M;
    if (N == 0 and M == 0)
      break;
    vector<vector<vector<int>>> dist(
        2, vector<vector<int>>(N, vector<int>(N, INF)));
    for (int i = 0; i < M; i++) {
      int x, y, t;
      char sl;
      cin >> x >> y >> t >> sl;
      x--;
      y--;
      int isl = (sl == 'L') ? 0 : 1;
      dist[isl][x][y] = min(dist[isl][x][y], t);
      dist[isl][y][x] = min(dist[isl][y][x], t);
    }
    for (int i = 0; i < N; i++) {
      dist[0][i][i] = dist[1][i][i] = 0;
    }
    for (int k = 0; k < N; k++) {
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          dist[0][i][j] = min(dist[0][i][j], dist[0][i][k] + dist[0][k][j]);
          dist[1][i][j] = min(dist[1][i][j], dist[1][i][k] + dist[1][k][j]);
        }
      }
    }
    int R;
    cin >> R;
    vector<int> togo(R);
    for (int i = 0; i < R; i++) {
      cin >> togo[i];
      togo[i]--;
    }
    vector<vector<int>> dp(R + 1, vector<int>(N, INF));
    dp[0][togo[0]] = 0;
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < N; j++) {
        if (dp[i][j] == INF)
          continue;
        for (int k = 0; k < N; k++) {
          dp[i + 1][k] =
              min(dp[i + 1][k], dp[i][j] + dist[0][togo[i]][j] + dist[1][j][k] +
                                    dist[0][k][togo[i + 1]]);
        }
      }
    }
    int ret = INF;
    for (int i = 0; i < N; i++) {
      ret = min(ret, dp[R - 1][i]);
    }
    cout << ret << endl;
  }
  return 0;
}