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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(S) (S).begin(), (S).end()
typedef long long ll;

const ll INF = 1LL << 50;
const int MAXN = 210;
const int MAXR = 1010;

ll l_dist[MAXN][MAXN];
ll s_dist[MAXN][MAXN];
ll dp[MAXR][MAXN];

int main() {
  ios::sync_with_stdio(false);

  int N, M, R;

  while (cin >> N >> M && N + M) {
    fill(&l_dist[0][0], &l_dist[0][0] + MAXN * MAXN, INF);
    fill(&s_dist[0][0], &s_dist[0][0] + MAXN * MAXN, INF);
    fill(&dp[0][0], &dp[0][0] + MAXR * MAXN, INF);

    REP(i, N) {
      l_dist[i][i] = 0;
      s_dist[i][i] = 0;
    }

    REP(i, M) {
      int x, y, t;
      string sl;

      cin >> x >> y >> t >> sl;
      x--, y--;
      if (sl == "L") {
        l_dist[x][y] = l_dist[y][x] = min(l_dist[x][y], (ll)t);
      } else {
        s_dist[x][y] = s_dist[y][x] = min(s_dist[x][y], (ll)t);
      }
    }

    REP(k, N) REP(i, N) REP(j, N) {
      l_dist[i][j] = min(l_dist[i][j], l_dist[i][k] + l_dist[k][j]);
      s_dist[i][j] = min(s_dist[i][j], s_dist[i][k] + s_dist[k][j]);
    }

    cin >> R;
    vector<int> zs(R);
    REP(i, R) {
      cin >> zs[i];
      zs[i]--;
    }

    dp[0][zs[0]] = 0;

    REP2(i, 1, R) REP(u, N) if (dp[i - 1][u] < INF) {
      // cout << i - 1 << " " << u << endl;
      int cz = zs[i - 1];
      int nz = zs[i];

      dp[i][u] = min(dp[i][u], dp[i - 1][u] + l_dist[cz][nz]);

      REP(v, N) {
        dp[i][v] = min(dp[i][v], dp[i - 1][u] + l_dist[cz][u] + s_dist[u][v] +
                                     l_dist[v][nz]);
      }
    }

    ll res = INF;
    REP(i, N) { res = min(res, dp[R - 1][i]); }
    cout << res << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(S) (S).begin(), (S).end()
typedef long long ll;

const int INF = 10000 * 10000;
const int MAXN = 210;

int l_dist[MAXN][MAXN];
int s_dist[MAXN][MAXN];
int dp[MAXN][MAXN];

int main() {
  ios::sync_with_stdio(false);

  int N, M, R;

  while (cin >> N >> M && N + M) {
    fill(&l_dist[0][0], &l_dist[0][0] + MAXN * MAXN, INF);
    fill(&s_dist[0][0], &s_dist[0][0] + MAXN * MAXN, INF);
    fill(&dp[0][0], &dp[0][0] + MAXN * MAXN, INF);

    REP(i, N) {
      l_dist[i][i] = 0;
      s_dist[i][i] = 0;
    }

    REP(i, M) {
      int x, y, t;
      string sl;

      cin >> x >> y >> t >> sl;
      x--, y--;
      if (sl == "L") {
        l_dist[x][y] = l_dist[y][x] = min(l_dist[x][y], t);
      } else {
        s_dist[x][y] = s_dist[y][x] = min(s_dist[x][y], t);
      }
    }

    REP(k, N) REP(i, N) REP(j, N) {
      l_dist[i][j] = min(l_dist[i][j], l_dist[i][k] + l_dist[k][j]);
      s_dist[i][j] = min(s_dist[i][j], s_dist[i][k] + s_dist[k][j]);
    }

    cin >> R;
    vector<int> zs(R);
    REP(i, R) {
      cin >> zs[i];
      zs[i]--;
    }

    dp[0][zs[0]] = 0;

    REP2(i, 1, R) REP(u, N) if (dp[i - 1][u] < INF) {
      // cout << i - 1 << " " << u << endl;
      int cz = zs[i - 1];
      int nz = zs[i];

      dp[i][u] = min(dp[i][u], dp[i - 1][u] + l_dist[cz][nz]);

      REP(v, N) {
        dp[i][v] = min(dp[i][v], dp[i - 1][u] + l_dist[cz][u] + s_dist[u][v] +
                                     l_dist[v][nz]);
      }
    }

    int res = INF;
    REP(i, N) { res = min(res, dp[R - 1][i]); }
    cout << res << endl;
  }

  return 0;
}