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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef ll li;
typedef pair<int, int> PI;
#define EPS (1e-10L)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP(i, n) rep(i, n)
#define F first
#define S second
#define mp(a, b) make_pair((a), (b))
#define list3(a, b, c) mp((a), mp((b), (c)))
#define list4(a, b, c, d) mp((a), list3((b), (c), (d)))
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define CLR(a) memset((a), 0, sizeof(a))
#define FOR(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &val) {
  return out << "(" << val.F << ", " << val.S << ")";
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &val) {
  out << "{";
  rep(i, SZ(val)) out << (i ? ", " : "") << val[i];
  return out << "}";
}
void pkuassert(bool t) { t = 1 / t; };
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};

int n, m;
ll scost[200][200];
ll lcost[200][200];
int z[1000];
ll cost[1000][200];

void solve() {
  rep(i, n) rep(j, n) scost[i][j] = lcost[i][j] = 1LL << 30LL;
  rep(i, n) scost[i][i] = lcost[i][i] = 0;
  rep(i, m) {
    ll x, y, t;
    char s;
    cin >> x >> y >> t >> s;
    --x, --y;
    if (s == 'S')
      scost[x][y] = scost[y][x] = min(scost[x][y], t);
    else
      lcost[x][y] = lcost[y][x] = min(lcost[x][y], t);
  }
  rep(k, n) rep(i, n) rep(j, n) {
    scost[i][j] = min(scost[i][j], scost[i][k] + scost[k][j]);
    lcost[i][j] = min(lcost[i][j], lcost[i][k] + lcost[k][j]);
  }

  int r;
  cin >> r;
  rep(i, r) {
    cin >> z[i];
    --z[i];
  }
  rep(i, r) rep(j, n) cost[i][j] = 1LL << 50LL;
  cost[0][z[0]] = 0;
  ll ans = 1LL << 60LL;
  rep(i, r - 1) rep(j, n) {
    rep(k, n) {
      ll nc = cost[i][j];
      if (k == j)
        nc += lcost[z[i]][z[i + 1]];
      else
        nc += lcost[z[i]][j] + scost[j][k] + lcost[k][z[i + 1]];
      cost[i + 1][k] = min(cost[i + 1][k], nc);
      if (i + 1 == r - 1)
        ans = min(ans, cost[i + 1][k]);
    }
  }
  cout << ans << endl;
}

int main() {
  while (cin >> n >> m && n)
    solve();
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef ll li;
typedef pair<int, int> PI;
#define EPS (1e-10L)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP(i, n) rep(i, n)
#define F first
#define S second
#define mp(a, b) make_pair((a), (b))
#define list3(a, b, c) mp((a), mp((b), (c)))
#define list4(a, b, c, d) mp((a), list3((b), (c), (d)))
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define CLR(a) memset((a), 0, sizeof(a))
#define FOR(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &val) {
  return out << "(" << val.F << ", " << val.S << ")";
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &val) {
  out << "{";
  rep(i, SZ(val)) out << (i ? ", " : "") << val[i];
  return out << "}";
}
void pkuassert(bool t) { t = 1 / t; };
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};

int n, m;
ll scost[200][200];
ll lcost[200][200];
int z[1000];
ll cost[1000][200];

void solve() {
  rep(i, n) rep(j, n) scost[i][j] = lcost[i][j] = 1LL << 30LL;
  rep(i, n) scost[i][i] = lcost[i][i] = 0;
  rep(i, m) {
    ll x, y, t;
    char s;
    cin >> x >> y >> t >> s;
    --x, --y;
    if (s == 'S')
      scost[x][y] = scost[y][x] = min(scost[x][y], t);
    else
      lcost[x][y] = lcost[y][x] = min(lcost[x][y], t);
  }
  rep(k, n) rep(i, n) rep(j, n) {
    scost[i][j] = min(scost[i][j], scost[i][k] + scost[k][j]);
    lcost[i][j] = min(lcost[i][j], lcost[i][k] + lcost[k][j]);
  }

  int r;
  cin >> r;
  rep(i, r) {
    cin >> z[i];
    --z[i];
  }
  rep(i, r + 1) rep(j, n) cost[i][j] = 1LL << 50LL;
  cost[0][z[0]] = 0;
  ll ans = 1LL << 60LL;
  rep(i, r - 1) rep(j, n) {
    rep(k, n) {
      ll nc = cost[i][j];
      if (k == j)
        nc += lcost[z[i]][z[i + 1]];
      else
        nc += lcost[z[i]][j] + scost[j][k] + lcost[k][z[i + 1]];
      cost[i + 1][k] = min(cost[i + 1][k], nc);
      if (i + 1 == r - 1)
        ans = min(ans, cost[i + 1][k]);
    }
  }
  cout << ans << endl;
}

int main() {
  while (cin >> n >> m && n)
    solve();
}