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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const ll INF = 1LL << 50;
ll land[200][200];
ll sea[200][200];
ll dp[1010][200];
int z[1000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M, R;
  while (cin >> N >> M, N) {
    fill((ll *)begin(land), (ll *)end(land), INF);
    fill((ll *)begin(sea), (ll *)end(sea), INF);
    for (int i = 0; i < N; i++) {
      land[i][i] = sea[i][i] = 0;
    }

    for (int i = 0; i < M; i++) {
      int x, y;
      ll t;
      char sl;
      cin >> x >> y >> t >> sl;
      x--, y--;
      if (sl == 'L') {
        t = min(t, land[x][y]);
        land[x][y] = land[y][x] = t;
      } else {
        t = min(t, sea[x][y]);
        sea[x][y] = sea[y][x] = t;
      }
    }

    for (int k = 0; k < N; k++) {
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          land[i][j] = min(land[i][j], land[i][k] + land[k][j]);
          sea[i][j] = min(sea[i][j], sea[i][k] + sea[k][j]);
        }
      }
    }

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

    if (R == 1) {
      cout << 0 << endl;
      continue;
    }

    fill((ll *)begin(dp), (ll *)end(dp), INF);
    dp[0][z[0]] = 0;
    for (int i = 0; i < R - 1; i++) {
      for (int j = 0; j < N; j++) {
        if (dp[i][j] == INF)
          continue;
        int A = z[i], B = z[i + 1];

        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + land[A][B]);

        for (int u = 0; u < N; u++) {
          ll cost = land[A][j] + sea[j][u] + land[u][B];
          if (cost < INF) {
            dp[i + 1][u] = min(dp[i + 1][u], dp[i][j] + cost);
          }
        }
      }
    }

    ll ans = INF;
    for (int j = 0; j < N; j++) {
      ans = min(ans, dp[R - 1][j]);
    }
    assert(ans < INF);
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const ll INF = 1LL << 50;
ll land[200][200];
ll sea[200][200];
ll dp[1010][200];
int z[1000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M, R;
  while (cin >> N >> M, N) {
    fill((ll *)begin(land), (ll *)end(land), INF);
    fill((ll *)begin(sea), (ll *)end(sea), INF);
    for (int i = 0; i < N; i++) {
      land[i][i] = sea[i][i] = 0;
    }

    for (int i = 0; i < M; i++) {
      int x, y;
      ll t;
      char sl;
      cin >> x >> y >> t >> sl;
      x--, y--;
      if (sl == 'L') {
        t = min(t, land[x][y]);
        land[x][y] = land[y][x] = t;
      } else {
        t = min(t, sea[x][y]);
        sea[x][y] = sea[y][x] = t;
      }
    }

    for (int k = 0; k < N; k++) {
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          land[i][j] = min(land[i][j], land[i][k] + land[k][j]);
          sea[i][j] = min(sea[i][j], sea[i][k] + sea[k][j]);
        }
      }
    }

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

    if (R == 1) {
      cout << 0 << endl;
      continue;
    }

    fill((ll *)begin(dp), (ll *)end(dp), INF);
    dp[z[0]][z[0]] = 0;
    for (int i = 0; i < R - 1; i++) {
      for (int j = 0; j < N; j++) {
        if (dp[i][j] == INF)
          continue;
        int A = z[i], B = z[i + 1];

        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + land[A][B]);

        for (int u = 0; u < N; u++) {
          ll cost = land[A][j] + sea[j][u] + land[u][B];
          if (cost < INF) {
            dp[i + 1][u] = min(dp[i + 1][u], dp[i][j] + cost);
          }
        }
      }
    }

    ll ans = INF;
    for (int j = 0; j < N; j++) {
      ans = min(ans, dp[R - 1][j]);
    }
    assert(ans < INF);
    cout << ans << endl;
  }
}