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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef complex<ld> compd;
#define rep(i, n) for (int i = 0; i < n; i++)
#define srep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) for (int i = 0; i <= n; i++)
#define SREP(i, a, n) for (int i = a; i <= n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define RREP(i, n) for (int i = n; i >= 0; i--)
#define all(a) (a).begin(), (a).end()
#define mp(a, b) make_pair(a, b)
#define mt make_tuple
#define fst first
#define scn second
#define bicnt(x) __buildin__popcount(x)
#define debug(x) cout << "debug: " << x << endl
#define DEBUG 0

const ll inf = (ll)1e8;
const ll mod = (ll)1e9 + 7;
const ld eps = 1e-18;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

int ldist[210][210];
int sdist[210][210];
int dp[210], dp2[210];

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      break;
    rep(i, n) rep(j, n) ldist[i][j] = sdist[i][j] = (i == j ? 0 : inf);
    rep(i, m) {
      int x, y, t;
      char sl;
      cin >> x >> y >> t >> sl;
      x--;
      y--;
      if (sl == 'L')
        ldist[x][y] = ldist[y][x] = min(ldist[x][y], t);
      else
        sdist[x][y] = sdist[y][x] = min(sdist[x][y], t);
    }
    rep(k, n) rep(i, n) rep(j, n) {
      ldist[i][j] = min(ldist[i][j], ldist[i][k] + ldist[k][j]);
      sdist[i][j] = min(sdist[i][j], sdist[i][k] + sdist[k][j]);
    }
    int r;
    cin >> r;
    vint z(r);
    rep(i, r) {
      cin >> z[i];
      z[i]--;
    }
    rep(i, n) dp[i] = (i == z[0] ? 0 : inf);
    srep(i, 1, r) {
      rep(j, n) dp2[j] = dp[j] + ldist[z[i - 1]][z[i]];
      rep(j, n) rep(k, n) {
        dp2[j] = min(dp2[j],
                     dp[k] + ldist[z[i - 1]][k] + sdist[k][j] + ldist[j][z[i]]);
      }
      rep(j, n) dp[j] = dp2[j];
    }
    int ret = inf;
    rep(i, n) ret = min(ret, dp[i]);
    cout << ret << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef complex<ld> compd;
#define rep(i, n) for (int i = 0; i < n; i++)
#define srep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) for (int i = 0; i <= n; i++)
#define SREP(i, a, n) for (int i = a; i <= n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define RREP(i, n) for (int i = n; i >= 0; i--)
#define all(a) (a).begin(), (a).end()
#define mp(a, b) make_pair(a, b)
#define mt make_tuple
#define fst first
#define scn second
#define bicnt(x) __buildin__popcount(x)
#define debug(x) cout << "debug: " << x << endl
#define DEBUG 0

const ll inf = (ll)1e8;
const ll mod = (ll)1e9 + 7;
const ld eps = 1e-18;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

int ldist[210][210];
int sdist[210][210];
int dp[210], dp2[210];

int main() {
  ifstream ifs("input.txt");
  while (true) {
    int n, m;
    ifs >> n >> m;
    if (n == 0)
      break;
    rep(i, n) rep(j, n) ldist[i][j] = sdist[i][j] = (i == j ? 0 : inf);
    rep(i, m) {
      int x, y, t;
      char sl;
      ifs >> x >> y >> t >> sl;
      x--;
      y--;
      if (sl == 'L') {
        ldist[x][y] = ldist[y][x] = min(ldist[x][y], t);
      } else {
        sdist[x][y] = sdist[y][x] = min(sdist[x][y], t);
      }
    }
    rep(k, n) rep(i, n) rep(j, n) {
      ldist[i][j] = min(ldist[i][j], ldist[i][k] + ldist[k][j]);
      sdist[i][j] = min(sdist[i][j], sdist[i][k] + sdist[k][j]);
    }
    int r;
    ifs >> r;
    vint z(r);
    rep(i, r) {
      ifs >> z[i];
      z[i]--;
    }
    rep(i, n) dp[i] = ldist[i][z[0]];
    rep(i, n) dp2[i] = inf;
    srep(i, 1, r) {
      rep(j, n) dp2[j] = dp[j] + ldist[z[i - 1]][z[i]];
      rep(j, n) rep(k, n) {
        dp2[j] = min(dp2[j],
                     dp[k] + ldist[z[i - 1]][k] + sdist[k][j] + ldist[j][z[i]]);
      }
      rep(j, n) {
        dp[j] = dp2[j];
        dp2[j] = inf;
      }
    }
    int ret = inf;
    rep(i, n) ret = min(ret, dp[i]);
    cout << ret << endl;
  }
  return 0;
}