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

#include <iostream>
#include <vector>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;

int main() {
  int N, M;
  while (cin >> N >> M, N) {
    vector<vector<long long>> dps(N, vector<long long>(N, INF)),
        dpl(N, vector<long long>(N, INF));
    for (int v = 0; v < N; ++v)
      dps[v][v] = dpl[v][v] = 0;
    for (int e = 0; e < M; ++e) {
      int x, y;
      long long w;
      char type;
      cin >> x >> y >> w >> type;
      --x, --y;
      if (type == 'L')
        chmin(dpl[x][y], w), chmin(dpl[y][x], w);
      else
        chmin(dps[x][y], w), chmin(dps[y][x], w);
    }
    for (int k = 0; k < N; ++k) {
      for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
          dpl[i][j] = min(dpl[i][j], dpl[i][k] + dpl[k][j]);
          dps[i][j] = min(dps[i][j], dps[i][k] + dps[k][j]);
        }
      }
    }

    int R;
    cin >> R;
    vector<int> v(R);
    for (int i = 0; i < R; ++i)
      cin >> v[i], --v[i];

    vector<vector<long long>> dp(R, vector<long long>(N, INF));
    dp[0][v[0]] = 0;
    for (int i = 0; i < R - 1; ++i) {
      for (int j = 0; j < N; ++j) {
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + dpl[v[i]][v[i + 1]]);
        for (int k = 0; k < N; ++k) {
          dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + dpl[v[i]][j] + dps[j][k] +
                                               dpl[k][v[i + 1]]);
        }
      }
    }
    long long res = INF;
    for (int i = 0; i < N; ++i) {
      res = min(res, dp[R - 1][i]);
    }
    cout << res << endl;
  }
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;

int main() {
  int N, M;
  while (cin >> N >> M, N) {
    vector<vector<long long>> dps(N, vector<long long>(N, INF)),
        dpl(N, vector<long long>(N, INF));
    for (int v = 0; v < N; ++v)
      dps[v][v] = dpl[v][v] = 0;
    for (int e = 0; e < M; ++e) {
      int x, y;
      long long w;
      char type;
      cin >> x >> y >> w >> type;
      --x, --y;
      if (type == 'L')
        chmin(dpl[x][y], w), chmin(dpl[y][x], w);
      else
        chmin(dps[x][y], w), chmin(dps[y][x], w);
    }
    for (int k = 0; k < N; ++k) {
      for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
          dpl[i][j] = min(dpl[i][j], dpl[i][k] + dpl[k][j]);
          dps[i][j] = min(dps[i][j], dps[i][k] + dps[k][j]);
        }
      }
    }

    int R;
    cin >> R;
    vector<int> v(R);
    for (int i = 0; i < R; ++i)
      cin >> v[i], --v[i];

    vector<vector<long long>> dp(R, vector<long long>(N, INF));
    dp[0][0] = 0;
    for (int i = 0; i < R - 1; ++i) {
      for (int j = 0; j < N; ++j) {
        for (int k = 0; k < N; ++k) {
          dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + dpl[v[i]][j] + dps[j][k] +
                                               dpl[k][v[i + 1]]);
        }
      }
    }
    long long res = INF;
    for (int i = 0; i <= R - 1; ++i) {
      res = min(res, dp[R - 1][i]);
    }
    cout << res << endl;
  }
}