Problem p01317 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? 1e9 : 1e18)
#define EPS 0.0000000001
using namespace std;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <typename Head, typename Value>
auto vectors(const Head &head, const Value &v) {
  return vector<Value>(head, v);
}
template <typename Head, typename... Tail> auto vectors(Head x, Tail... tail) {
  auto inner = vectors(tail...);
  return vector<decltype(inner)>(x, inner);
}
template <class T> void join(T a) {
  int b = 0;
  for (auto itr : a) {
    if (b++ != 0)
      cout << " ";
    cout << itr;
  }
}
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pii>;
int W, H;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool valid(int x, int y) { return (0 <= x && x < W) && (0 <= y && y < H); }
#define int ll
signed main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    auto Ld = vectors(301, 301, (ll)INF);
    auto Sd = vectors(301, 301, (ll)INF);
    rep(i, n) Ld[i][i] = Sd[i][i] = 0;
    rep(i, m) {
      int a, b, c;
      char s;
      cin >> a >> b >> c >> s;
      --a, --b;
      if (s == 'L') {
        Ld[b][a] = Ld[a][b] = min(Ld[a][b], c);
      } else {
        Sd[b][a] = Sd[a][b] = min(Sd[a][b], c);
      }
    }
    rep(k, n) rep(i, n) rep(j, n) {
      cmin(Sd[i][j], Sd[i][k] + Sd[k][j]);
      cmin(Ld[i][j], Ld[i][k] + Ld[k][j]);
    }
    int r;
    cin >> r;
    vector<int> v;
    rep(i, r) {
      int t;
      cin >> t;
      v.push_back(--t);
    }
    auto dp = vectors(301, 301, (ll)INF);
    dp[0][v[0]] = 0;
    rep(i, r) {
      if (i == 0)
        continue;
      rep(j, n) {
        rep(k, n) {
          if (j != k)
            cmin(dp[i][j],
                 dp[i - 1][k] + Ld[v[i - 1]][k] + Sd[k][j] + Ld[j][v[i]]);
          else
            cmin(dp[i][j], dp[i - 1][j] + Ld[v[i - 1]][v[i]]);
        }
      }
    }
    int ans = INF;
    for (auto val : dp[r - 1])
      cmin(ans, val);
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? 1e9 : 1e18)
#define EPS 0.0000000001
using namespace std;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <typename Head, typename Value>
auto vectors(const Head &head, const Value &v) {
  return vector<Value>(head, v);
}
template <typename Head, typename... Tail> auto vectors(Head x, Tail... tail) {
  auto inner = vectors(tail...);
  return vector<decltype(inner)>(x, inner);
}
template <class T> void join(T a) {
  int b = 0;
  for (auto itr : a) {
    if (b++ != 0)
      cout << " ";
    cout << itr;
  }
}
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pii>;
int W, H;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool valid(int x, int y) { return (0 <= x && x < W) && (0 <= y && y < H); }
#define int ll
signed main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    auto Ld = vectors(301, 301, (ll)INF);
    auto Sd = vectors(301, 301, (ll)INF);
    rep(i, n) Ld[i][i] = Sd[i][i] = 0;
    rep(i, m) {
      int a, b, c;
      char s;
      cin >> a >> b >> c >> s;
      --a, --b;
      if (s == 'L') {
        Ld[b][a] = Ld[a][b] = min(Ld[a][b], c);
      } else {
        Sd[b][a] = Sd[a][b] = min(Sd[a][b], c);
      }
    }
    rep(k, n) rep(i, n) rep(j, n) {
      cmin(Sd[i][j], Sd[i][k] + Sd[k][j]);
      cmin(Ld[i][j], Ld[i][k] + Ld[k][j]);
    }
    int r;
    cin >> r;
    vector<int> v;
    rep(i, r) {
      int t;
      cin >> t;
      v.push_back(--t);
    }
    auto dp = vectors(2001, 2001, (ll)INF);
    dp[0][v[0]] = 0;
    rep(i, r) {
      if (i == 0)
        continue;
      rep(j, n) {
        rep(k, n) {
          if (j != k)
            cmin(dp[i][j],
                 dp[i - 1][k] + Ld[v[i - 1]][k] + Sd[k][j] + Ld[j][v[i]]);
          else
            cmin(dp[i][j], dp[i - 1][j] + Ld[v[i - 1]][v[i]]);
        }
      }
    }
    int ans = INF;
    for (auto val : dp[r - 1])
      cmin(ans, val);
    cout << ans << endl;
  }
}