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

#include <iostream>
#include <vector>
using namespace std;
const long long inf = 1LL << 55;
const int NODE = 210;
int main() {
  int n, m;
  while (cin >> n >> m && n && m) {
    long long sea[n][n];
    long long land[n][n];
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (i != j) {
          sea[i][j] = land[i][j] = inf;
        } else {
          sea[i][j] = land[i][j] = 0;
        }
      }
    }
    for (int i = 0; i < m; ++i) {
      int x, y;
      long long t;
      char sl;
      cin >> x >> y >> t >> sl;
      --x;
      --y;
      if (sl == 'S') {
        sea[x][y] = min(sea[x][y], t);
        sea[y][x] = min(sea[y][x], t);
      } else {
        land[x][y] = min(land[x][y], t);
        land[y][x] = min(land[y][x], t);
      }
    }
    for (int k = 0; k < n; ++k) {
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
          sea[i][j] = min(sea[i][j], sea[i][k] + sea[k][j]);
          land[i][j] = min(land[i][j], land[i][k] + land[k][j]);
        }
      }
    }

    int R;
    int cur, ship;
    int prez, z;
    long long preA[n][n];
    long long A[n][n];

    cin >> R;
    cin >> z;
    --z;
    cur = ship = z;
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        preA[j][k] = A[j][k] = inf;
      }
    }
    A[cur][ship] = 0;
    for (int i = 1; i < R; ++i) {
      for (int j = 0; j < n; ++j) {
        for (int k = 0; k < n; ++k) {
          preA[j][k] = A[j][k];
          A[j][k] = inf;
        }
      }
      prez = z;
      cin >> z;
      --z;
      for (int j = 0; j < n; ++j) {
        A[z][j] = min(A[z][j], preA[prez][j] + land[prez][z]);
        for (int k = 0; k < n; ++k) {
          A[z][k] = min(A[z][k],
                        preA[prez][j] + land[prez][j] + sea[j][k] + land[k][z]);
          // cout << z << ' ' << k << ' ' << A[z][k] << endl;
        }
      }
    }
    long long res = inf;
    for (int i = 0; i < n; ++i) {
      res = min(res, A[z][i]);
    }
    cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
const long long inf = 1LL << 55;
int main() {
  int n, m;
  while (cin >> n >> m && n && m) {
    long long sea[n][n];
    long long land[n][n];
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (i != j) {
          sea[i][j] = land[i][j] = inf;
        } else {
          sea[i][j] = land[i][j] = 0;
        }
      }
    }
    for (int i = 0; i < m; ++i) {
      int x, y;
      long long t;
      char sl;
      cin >> x >> y >> t >> sl;
      --x;
      --y;
      if (sl == 'S') {
        sea[x][y] = min(sea[x][y], t);
        sea[y][x] = min(sea[y][x], t);
      } else {
        land[x][y] = min(land[x][y], t);
        land[y][x] = min(land[y][x], t);
      }
    }
    for (int k = 0; k < n; ++k) {
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
          sea[i][j] = min(sea[i][j], sea[i][k] + sea[k][j]);
          land[i][j] = min(land[i][j], land[i][k] + land[k][j]);
        }
      }
    }

    int R;
    int cur, ship;
    int prez, z;
    cin >> R;
    cin >> z;
    long long A[R][200][200];
    --z;
    cur = ship = z;
    for (int i = 0; i < R; ++i) {
      for (int j = 0; j < 200; ++j) {
        for (int k = 0; k < 200; ++k) {
          A[i][j][k] = inf;
        }
      }
    }
    A[0][cur][ship] = 0;
    for (int i = 1; i < R; ++i) {
      prez = z;
      cin >> z;
      --z;
      for (int j = 0; j < n; ++j) {
        for (int k = 0; k < n; ++k) {
          A[i][z][k] = min(A[i][z][k], A[i - 1][prez][j] + land[prez][j] +
                                           sea[j][k] + land[k][z]);
        }
      }
    }
    long long res = inf;
    for (int i = 0; i < n; ++i) {
      res = min(res, A[R - 1][z][i]);
    }
    cout << res << endl;
  }
  return 0;
}