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

#include <bits/stdc++.h>
using namespace std;

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &a) {
  return is >> a.first >> a.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> &a) {
  return os << a.first << " " << a.second;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vc) {
  for (int i = 0; i < vc.size(); i++)
    is >> vc[i];
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vc) {
  for (int i = 0; i < vc.size(); i++)
    os << vc[i] << endl;
  return os;
}

#define ForEach(it, c)                                                         \\
  for (__typeof(c).begin() it = (c).begin(); it != (c).end(); it++)
#define ALL(v) (v).begin(), (v).end()
#define UNQ(s)                                                                 \\
  {                                                                            \\
    sort(ALL(s));                                                              \\
    (s).erase(unique(ALL(s)), (s).end());                                      \\
  }
#define fr first
#define sc second

typedef pair<int, int> Pi;
typedef pair<int, Pi> Pii;

typedef long long int64;
const int64 INF = 1LL << 55;

void Floyd_Warshall(vector<vector<int64>> &graph) {
  for (int k = 0; k < graph.size(); k++) {
    for (int i = 0; i < graph.size(); i++) {
      for (int j = 0; j < graph.size(); j++) {
        graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]);
      }
    }
  }
  return;
}

int main() {
  int N, M, R, z[1000];
  vector<vector<int64>> Road_graph, Sea_graph;
  int64 dp[1000][200];

  while (cin >> N >> M, N) {

    Road_graph = vector<vector<int64>>(N, vector<int64>(N, INF));
    Sea_graph = vector<vector<int64>>(N, vector<int64>(N, INF));
    for (int i = 0; i < N; i++) {
      Road_graph[i][i] = Sea_graph[i][i] = 0;
    }

    for (int i = 0; i < M; i++) {
      int x, y, t;
      char sl;
      cin >> x >> y >> t >> sl;
      --x, --y;
      if (sl == 'L')
        Road_graph[x][y] = Road_graph[y][x] = t;
      else
        Sea_graph[x][y] = Sea_graph[y][x] = t;
    }
    cin >> R;
    for (int i = 0; i < R; i++) {
      cin >> z[i];
      z[i]--;
    }

    Floyd_Warshall(Road_graph);
    Floyd_Warshall(Sea_graph);

    // dp[idx][船の場所]
    fill_n(*dp, 1000 * 200, INF);
    dp[0][z[0]] = 0;
    for (int i = 0; i < R - 1; i++) {
      for (int j = 0; j < N; j++) { // 船の場所
        if (dp[i][j] == INF)
          continue;
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + Road_graph[z[i]][z[i + 1]]);
        for (int k = 0; k < N; k++) {
          int64 Cost =
              Road_graph[z[i]][j] + Sea_graph[j][k] + Road_graph[k][z[i + 1]];
          if (Cost > INF)
            continue;
          dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + Cost);
        }
      }
    }
    int64 ret = INF;
    for (int i = 0; i < N; i++) {
      ret = min(ret, dp[R - 1][i]);
    }
    cout << ret << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 28;

int main() {
  int N, M;
  int city[2][200][200];
  int dp[2][200];
  int R, z[1000];

  while (cin >> N >> M, N) {

    fill_n(**city, 2 * 200 * 200, INF);
    fill_n(*dp, 2 * 200, INF);

    for (int i = 0; i < M; i++) {
      int a, b, t;
      char c;
      cin >> a >> b >> t >> c;
      a--, b--;
      if (c == 'L') {
        city[0][a][b] = city[0][b][a] = t;
      } else {
        city[1][a][b] = city[1][b][a] = t;
      }
    }
    cin >> R;
    for (int i = 0; i < R; i++) {
      cin >> z[i];
      z[i]--;
    }

    for (int l = 0; l < 2; l++) {
      for (int k = 0; k < N; k++) {
        city[l][k][k] = 0;
        for (int i = 0; i < N; i++) {
          for (int j = 0; j < N; j++) {
            city[l][i][j] = min(city[l][i][j], city[l][i][k] + city[l][k][j]);
          }
        }
      }
    }
    dp[0][z[0]] = 0;
    for (int i = 0; i < R - 1; i++) {
      for (int j = 0; j < N; j++)
        dp[!(i & 1)][j] = INF;
      for (int j = 0; j < N; j++) { // 船を止めてある場所
        if (dp[i & 1][j] == INF)
          continue;
        for (int k = 0; k < N; k++) { // 次船を止める場所
          if (j == k) {
            dp[!(i & 1)][k] =
                min(dp[!(i & 1)][k], dp[i & 1][j] + city[0][z[i]][z[i + 1]]);
          } else {
            dp[!(i & 1)][k] =
                min(dp[!(i & 1)][k], dp[i & 1][j] + city[0][z[i]][j] +
                                         city[1][j][k] + city[0][k][z[i + 1]]);
          }
        }
      }
    }
    cout << *min_element(dp[!(R & 1)], dp[R & 1]) << endl;
  }
  return 0;
}