Problem p01317 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const ll inf = 0x1f1f1f1f1f1f1f1f; //?造了一个三倍之后不会溢出的?.....
const ll maxn = 205;
ll land[maxn][maxn], sea[maxn][maxn], num[maxn], cost[maxn][maxn];
void init(int n) {
  memset(land, inf, sizeof(land));
  memset(sea, inf, sizeof(sea));
  memset(cost, inf, sizeof(cost));
  for (ll i = 1; i <= n; ++i) {
    sea[i][i] = land[i][i] = 0;
  }
}
ll slove(ll n, ll m, ll w) {
  for (ll k = 1; k <= n; ++k) {
    for (ll i = 1; i <= n; ++i) {
      for (ll j = 1; 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]);
      }
    }
  }
  cost[1][num[1]] = 0;
  for (ll i = 1; i <= w; ++i) {
    for (ll j = 1; j <= n; ++j) {
      cost[i][j] = min(cost[i][j], cost[i - 1][j] + land[num[i - 1]][num[i]]);
      for (ll k = 1; k <= n; ++k) {
        cost[i][k] = min(cost[i][k], cost[i - 1][j] + land[num[i - 1]][j] +
                                         sea[j][k] + land[k][num[i]]);
      }
    }
  }
  ll ans = inf;
  for (ll i = 1; i <= n; ++i) {
    ans = min(ans, cost[w][i]);
  }
  // return *min_element(cost[w],cost[w]+n+1);
  return ans;
}
int main() {
  ll n, m;
  while (scanf("%lld%lld", &n, &m), n | m) {
    init(n);
    for (ll i = 0; i < m; ++i) {
      ll a, b, c;
      char ch[10];
      scanf("%lld%lld%lld%s", &a, &b, &c, ch);
      if (ch[0] == 'S') {
        sea[a][b] = sea[b][a] = min(sea[a][b], c);
      } else {
        land[a][b] = land[b][a] = min(land[a][b], c);
      }
    }
    ll w;
    scanf("%lld", &w);
    for (ll i = 1; i <= w; ++i) {
      scanf("%lld", &num[i]);
    }
    printf("%lld\n", slove(n, m, w));
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const ll inf = 0x1f1f1f1f1f1f1f1f; //?造了一个三倍之后不会溢出的?.....
const ll maxn = 205;
ll land[maxn][maxn], sea[maxn][maxn], num[maxn * 5], cost[maxn * 5][maxn];
void init(int n) {
  memset(land, inf, sizeof(land));
  memset(sea, inf, sizeof(sea));
  memset(cost, inf, sizeof(cost));
  for (ll i = 1; i <= n; ++i) {
    sea[i][i] = land[i][i] = 0;
  }
}
ll slove(ll n, ll m, ll w) {
  for (ll k = 1; k <= n; ++k) {
    for (ll i = 1; i <= n; ++i) {
      for (ll j = 1; 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]);
      }
    }
  }
  cost[1][num[1]] = 0;
  for (ll i = 1; i <= w; ++i) {
    for (ll j = 1; j <= n; ++j) {
      cost[i][j] = min(cost[i][j], cost[i - 1][j] + land[num[i - 1]][num[i]]);
      for (ll k = 1; k <= n; ++k) {
        cost[i][k] = min(cost[i][k], cost[i - 1][j] + land[num[i - 1]][j] +
                                         sea[j][k] + land[k][num[i]]);
      }
    }
  }
  ll ans = inf;
  for (ll i = 1; i <= n; ++i) {
    ans = min(ans, cost[w][i]);
  }
  // return *min_element(cost[w],cost[w]+n+1);
  return ans;
}
int main() {
  ll n, m;
  while (scanf("%lld%lld", &n, &m), n | m) {
    init(n);
    for (ll i = 0; i < m; ++i) {
      ll a, b, c;
      char ch[10];
      scanf("%lld%lld%lld%s", &a, &b, &c, ch);
      if (ch[0] == 'S') {
        sea[a][b] = sea[b][a] = min(sea[a][b], c);
      } else {
        land[a][b] = land[b][a] = min(land[a][b], c);
      }
    }
    ll w;
    scanf("%lld", &w);
    for (ll i = 1; i <= w; ++i) {
      scanf("%lld", &num[i]);
    }
    printf("%lld\n", slove(n, m, w));
  }
  return 0;
}