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

#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int n, m, R;
long long land[300][300], water[300][300], dp[1005][300];
int a[1005];
long long min(long long a, long long b) {
  if (a < b)
    return a;
  else
    return b;
}
void pre() {
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++) {
      land[i][j] = land[j][i] = inf;
      water[i][j] = water[j][i] = inf;
    }
  for (int i = 1; i <= n; i++)
    land[i][i] = 0, water[i][i] = 0;
}

void floyd() {
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) {
        if (k == i || k == j || i == j)
          continue;
        water[i][j] = min(water[i][j], water[i][k] + water[k][j]);
        land[i][j] = min(land[i][j], land[i][k] + land[k][j]);
      }
}

int main() {
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    pre();
    for (int i = 1; i <= m; i++) {
      int l, r;
      string str;
      long long w;
      scanf("%d%d%lld", &l, &r, &w);
      cin >> str;
      if (str == "L")
        land[l][r] = land[r][l] = min(land[r][l], w);
      else
        water[l][r] = water[r][l] = min(water[r][l], w);
    }
    floyd();
    scanf("%d", &R);
    for (int i = 1; i <= R; i++)
      for (int j = 1; j <= n; j++)
        dp[i][j] = inf;
    for (int i = 1; i <= R; i++)
      scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++)
      dp[1][i] =
          water[a[1]][i] +
          land[i][a[1]]; // 第一次出发可以将船停到任意处再坐船回到当前目标点
    for (int i = 2; i <= R; i++)   // 目的地
      for (int j = 1; j <= n; j++) // 上一次的下船点
      {
        dp[i][j] = min(dp[i][j], land[a[i - 1]][a[i]] + dp[i - 1][j]); // 走陆地
        for (int k = 1; k <= n; k++) // 新的下船点
          dp[i][k] = min(dp[i][k], dp[i - 1][j] + land[a[i - 1]][j] +
                                       water[j][k] + land[k][a[i]]); // 走水路
      }

    long long minn = inf;
    for (int i = 1; i <= n; i++)
      minn = min(minn, dp[R][i]);
    printf("%lld\\n", minn);
  }
  return 0;
}


Error: -11

Fixed submission:

#include <cstring>
#include <iostream>
#include <string>

using namespace std;

const int N = 2100;

int main() {
  int l[N][N], s[N][N], v, e, r, tar[1010];

  while (cin >> v >> e && e + v) {
    char ch;
    int x, y, z;
    memset(l, 0x3f3f, sizeof l); // l,s初始化
    memset(s, 0x3f3f, sizeof s);
    long long dp[N][N]; // dp[i][j]表示到了第i个点,船在j
    memset(dp, 0x3f3f, sizeof dp);
    memset(tar, 0, sizeof tar);

    for (int i = 1; i <= e; i++) {
      cin >> x >> y >> z >> ch;
      if (ch == 'L')
        l[x][y] = l[y][x] = min(l[x][y], z);
      else
        s[x][y] = s[y][x] = min(s[x][y], z);
    }
    cin >> r;
    for (int i = 1; i <= r; i++)
      cin >> tar[i];

    for (int i = 1; i <= v; i++)
      l[i][i] = s[i][i] = 0;
    for (int k = 1; k <= v; k++)
      for (int i = 1; i <= v; i++)
        for (int j = 1; j <= v; j++) {
          l[i][j] = min(l[i][j], l[i][k] + l[k][j]);
          s[i][j] = min(s[i][j], s[i][k] + s[k][j]);
        }

    dp[1][tar[1]] = 0; // 人在1,船在第一个地点

    for (int i = 2; i <= r; i++)
      for (int j = 1; j <= v; j++) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + l[tar[i - 1]][tar[i]]);
        for (int k = 1; k <= v; k++)
          dp[i][j] = min(
              dp[i][j],
              dp[i - 1][k] + l[tar[i - 1]][k] + s[k][j] +
                  l[j]
                   [tar[i]]); // 到了i-1个点，回k处坐船去j,从j走到i,就是dp[i][j]
      }

    long long ans = 0x3f3f3f;
    for (int i = 1; i <= v; i++)
      ans = min(ans, dp[r][i]);
    cout << ans << endl;
  }
  return 0;
}