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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define RG register
#define IL inline
#define pi acos(-1.0)

#define inf 1e8
using namespace std;

int n, m, R;
int land[300][300], water[300][300], dp[1005][300];
int a[1005];

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;
      int w;
      scanf("%d%d%d", &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 = 1; i <= R; ++i) {
      for (int j = 1; j <= n; ++j) {
        if (dp[i][j] != inf) {
          dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + land[a[i]][a[i + 1]]);
          for (int k = 1; k <= n; ++k) {
            dp[i + 1][k] =
                min(dp[i + 1][k],
                    dp[i][j] + land[a[i]][j] + water[j][k] + land[a[i + 1]][k]);
          }
        }
      }
    }
    int minn = inf;
    for (int i = 1; i <= n; i++)
      minn = min(minn, dp[R][i]);
    printf("%d\\n", minn);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define inf 0x3f3f3f3f
LL e1[220][220], e2[220][220];
LL f[1100][220], Iinf;
int a[220];
void init(int N) {
  memset(e1, inf, sizeof(e1));
  memset(e2, inf, sizeof(e2));
  memset(f, inf, sizeof(f));
  Iinf = f[1][1];
  for (int i = 1; i <= N; ++i)
    e1[i][i] = e2[i][i] = 0;
}
int main() {
  int N, M, i, j, k, u, v, R;
  LL w;
  char str[10];
  while (scanf("%d%d", &N, &M) == 2) {
    if (N == 0 && M == 0)
      break;
    init(N);
    while (M--) {
      scanf("%d%d%lld", &u, &v, &w);
      scanf("%s", str);
      if (!strcmp(str, "L")) {
        if (w < e1[u][v]) {
          e1[u][v] = e1[v][u] = w;
        }
      } else {
        if (w < e2[u][v]) {
          e2[u][v] = e2[v][u] = w;
        }
      }
    }
    // floyd(e1,N);
    // floyd(e2,N);
    for (i = 1; i <= N; ++i) {
      for (j = 1; j <= N; ++j) {
        if (e1[i][j] == Iinf)
          continue;
        for (k = 1; k <= N; ++k) {
          e1[i][k] = min(e1[i][k], e1[i][j] + e1[j][k]);
        }
      }
    }

    for (i = 1; i <= N; ++i) {
      for (j = 1; j <= N; ++j) {
        if (e2[i][j] == Iinf)
          continue;
        for (k = 1; k <= N; ++k) {
          e2[i][k] = min(e2[i][k], e2[i][j] + e2[j][k]);
        }
      }
    }

    scanf("%d", &R);
    for (i = 1; i <= R; ++i)
      scanf("%d", a + i);
    for (i = 1; i <= N; ++i)
      f[1][i] = min(f[1][i], e1[i][a[1]] + e2[i][a[1]]);
    for (i = 1; i < R; ++i) {
      for (j = 1; j <= N; ++j) {
        if (f[i][j] != Iinf) {
          for (k = 1; k <= N; ++k) {
            if (e2[j][k] != Iinf && e1[a[i]][j] != Iinf &&
                e1[a[i + 1]][k] != Iinf)
              f[i + 1][k] = min(f[i + 1][k], f[i][j] + e1[a[i]][j] +
                                                 e1[a[i + 1]][k] + e2[j][k]);
          }
        }
        f[i + 1][j] = min(f[i + 1][j], f[i][j] + e1[a[i]][a[i + 1]]);
      }
    }
    LL ans = Iinf;
    for (i = 1; i <= N; ++i)
      ans = min(ans, f[R][i]);
    cout << ans << endl;
  }
  return 0;
}