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

#include <algorithm>
#include <iostream>

using namespace std;

const int inf = 1e8;
int s[1111];
int l[222][222], w[222][222];
int dp[1111][222];

int main(int argc, char const *argv[]) {
  // freopen("in.txt", "r", stdin);
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  while (cin >> n >> m && n) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (i == j) {
          l[i][j] = 0, w[i][j] = 0;
        } else {
          l[i][j] = inf, w[i][j] = inf;
        }
      }
    }
    for (int i = 0; i < m; i++) {
      int u, v, t;
      char op[2];
      cin >> u >> v >> t >> op;
      if (op[0] == 'L') {
        l[u][v] = min(l[u][v], t);
        l[v][u] = l[u][v];
      } else {
        w[u][v] = min(w[u][v], t);
        w[v][u] = w[u][v];
      }
    }
    for (int k = 1; k <= n; k++) {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
          l[i][j] = min(l[i][j], l[i][k] + l[k][j]);
          w[i][j] = min(w[i][j], w[i][k] + w[k][j]);
        }
      }
    }

    int len;
    cin >> len;
    // cout << len << endl;
    for (int i = 1; i <= len; i++) {
      cin >> s[i];
    }
    for (int i = 1; i <= len; i++) {
      for (int j = 1; j <= n; j++) {
        dp[i][j] = inf;
      }
    }
    for (int i = 1; i <= n; i++) {
      dp[1][i] = w[s[1]][i] + l[i][s[1]];
    }
    for (int i = 2; i <= len; i++) {
      for (int j = 1; j <= n; j++) {
        for (int k = 1; k <= n; k++) {
          dp[i][j] = min(dp[i][j],
                         dp[i - 1][k] + l[s[i - 1]][k] + w[k][j] + l[j][s[i]]);
          if (j == k)
            dp[i][j] = min(dp[i][j], dp[i - 1][k] + l[s[i - 1]][s[i]]);
        }
      }
    }
    /*for(int i = 1; i <= len; i++){
            for(int j = 1; j <= n; j++){
                    cout << dp[i][j] << ' ';
            }
            cout << endl;
    }*/
    cout << *min_element(dp[len] + 1, dp[len] + n + 1) << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

int l[222][222], w[222][222];
int s[1111];
const int inf = 2e8;
int n, m;
int dp[1111][222];

void floyd(int a[222][222]) {
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
      }
    }
  }
}

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  while (cin >> n >> m && n) {
    for (int i = 1; i <= n; i++) {
      fill(l[i], l[i] + n + 1, inf);
      fill(w[i], w[i] + n + 1, inf);
      l[i][i] = 0;
      w[i][i] = 0;
    }
    for (int i = 0; i < m; i++) {
      int u, v, t;
      char op[2];
      cin >> u >> v >> t >> op;
      // cout << u << endl;
      if (op[0] == 'L') {
        l[u][v] = min(l[u][v], t);
        l[v][u] = min(l[v][u], t);
      } else {
        w[u][v] = min(w[u][v], t);
        w[v][u] = min(w[v][u], t);
      }
    }

    floyd(l);
    /*for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                    cout << l[i][j] << ' ';
            }
            cout << endl;
    }*/
    floyd(w);
    int len;
    cin >> len;
    for (int i = 1; i <= len; i++) {
      cin >> s[i];
    }
    for (int i = 1; i <= len; i++) {
      fill(dp[i], dp[i] + n + 1, inf);
    }
    for (int i = 1; i <= n; i++) {
      dp[0][i] = w[1][i];
    }
    for (int i = 1; i <= len; i++) {
      for (int j = 1; j <= n; j++) {
        for (int k = 1; k <= n; k++) {
          dp[i][j] = min(dp[i][j],
                         dp[i - 1][k] + l[s[i - 1]][k] + w[k][j] + l[j][s[i]]);
        }
      }
    }
    cout << *min_element(dp[len] + 1, dp[len] + n + 1) << endl;
  }
  return 0;
}