Problem p01317 (C++) — buggy submission, verdict: 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, INF);
    auto Sd = vectors(301, 301, 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') {
        cmin(Ld[a][b], c);
        Ld[b][a] = Ld[a][b];
      } else {
        cmin(Sd[a][b], c);
        Sd[b][a] = Sd[a][b];
      }
    }
    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(1000 + 1, 300 + 1, (int)INF);
    dp[0][v[0]] = 0;
    rep(i, r - 1) {
      /*if(i == 0){
          rep(k,n)dp[0][k] = Ld[v[i]][k] + Sd[v[i]][k] + Ld[k][v[i]];
      }*/
      rep(j, n) {
        cmin(dp[i + 1][j], dp[i][j] + Ld[v[i]][v[i + 1]]);
        rep(k, n) {
          cmin(dp[i + 1][k],
               dp[i][j] + Ld[v[i]][j] + Sd[j][k] + Ld[k][v[i + 1]]);
        }
      }
    }
    int ans = INF;
    rep(i, n) cmin(ans, dp[r - 1][i]);
    cout << ans << endl;
  }
}


Error: 0

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, INF);
    auto Sd = vectors(301, 301, 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') {
        cmin(Ld[a][b], c);
        Ld[b][a] = Ld[a][b];
      } else {
        cmin(Sd[a][b], c);
        Sd[b][a] = Sd[a][b];
      }
    }
    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(300 + 1, 300 + 1, (int)INF);
    dp[0][v[0]] = 0;
    rep(i, r - 1) {
      if (i == 0) {
        rep(k, n) dp[0][k] = Ld[v[i]][k] + Sd[v[i]][k] + Ld[k][v[i]];
      }
      rep(j, n) {
        rep(k, n) {
          cmin(dp[i + 1][j],
               dp[i][k] + Ld[v[i]][k] + Sd[k][j] + Ld[j][v[i + 1]]);
        }
      }
    }
    int ans = INF;
    rep(i, n) cmin(ans, dp[n - 1][i]);
    cout << ans << endl;
  }
}