Problem p01317 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 205
#define maxr 1005
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
int fl[maxn][maxn], fs[maxn][maxn], dp[maxr][maxn], n, m, r, a[maxn];
int main() {
  while (~scanf("%d%d", &n, &m) && (n + m) > 0) {
    memset(fl, 0x3f, sizeof(fl));
    memset(fs, 0x3f, sizeof(fs));
    for (int i = 1; i <= n; i++)
      fs[i][i] = fl[i][i] = 0;
    for (int i = 1; i <= m; i++) {
      int u, v, w;
      scanf("%d%d%d", &u, &v, &w);
      char c;
      while (scanf("%c", &c) && (c != 'S' && c != 'L'))
        ;
      if (c == 'S')
        fs[u][v] = fs[v][u] = min(fs[u][v], w);
      else
        fl[u][v] = fl[v][u] = min(fl[u][v], w);
    }
    scanf("%d", &r);
    for (int i = 1; i <= r; i++)
      scanf("%d", &a[i]);
    for (int k = 1; k <= n; k++) {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
          fl[i][j] = min(fl[i][j], fl[i][k] + fl[k][j]);
          fs[i][j] = min(fs[i][j], fs[i][k] + fs[k][j]);
        }
      }
    }
    memset(dp, 0x3f, sizeof(dp));
    //		a[0] = 1;
    for (int i = 1; i <= n; i++)
      dp[1][i] = fs[a[1]][i] + fl[i][a[1]];
    for (int i = 2; i <= r; i++) {
      for (int j = 1; j <= n; j++) {
        for (int k = 1; k <= n; k++) {
          ll res;
          if (j == k)
            res = (ll)dp[i - 1][k] + (ll)fl[a[i - 1]][a[i]];
          else
            res = (ll)dp[i - 1][k] + (ll)fl[a[i - 1]][k] + (ll)fs[k][j] +
                  (ll)fl[j][a[i]];
          dp[i][j] = min((ll)dp[i][j], res);
        }
      }
    }
    int ans = inf;
    for (int i = 1; i <= n; i++) {
      ans = min(ans, dp[r][i]);
    }
    printf("%d\n", ans);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 205
#define maxr 1005
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
int fl[maxn][maxn], fs[maxn][maxn], dp[maxr][maxn], n, m, r, a[maxr];
int main() {
  while (~scanf("%d%d", &n, &m) && (n + m) > 0) {
    memset(fl, 0x3f, sizeof(fl));
    memset(fs, 0x3f, sizeof(fs));
    for (int i = 1; i <= n; i++)
      fs[i][i] = fl[i][i] = 0;
    for (int i = 1; i <= m; i++) {
      int u, v, w;
      scanf("%d%d%d", &u, &v, &w);
      char c;
      while (scanf("%c", &c) && (c != 'S' && c != 'L'))
        ;
      if (c == 'S')
        fs[u][v] = fs[v][u] = min(fs[u][v], w);
      else
        fl[u][v] = fl[v][u] = min(fl[u][v], w);
    }
    scanf("%d", &r);
    for (int i = 1; i <= r; i++)
      scanf("%d", &a[i]);
    for (int k = 1; k <= n; k++) {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
          fl[i][j] = min(fl[i][j], fl[i][k] + fl[k][j]);
          fs[i][j] = min(fs[i][j], fs[i][k] + fs[k][j]);
        }
      }
    }
    memset(dp, 0x3f, sizeof(dp));
    //		a[0] = 1;
    for (int i = 1; i <= n; i++)
      dp[1][i] = fs[a[1]][i] + fl[i][a[1]];
    for (int i = 2; i <= r; i++) {
      for (int j = 1; j <= n; j++) {
        for (int k = 1; k <= n; k++) {
          ll res;
          if (j == k)
            res = (ll)dp[i - 1][k] + (ll)fl[a[i - 1]][a[i]];
          else
            res = (ll)dp[i - 1][k] + (ll)fl[a[i - 1]][k] + (ll)fs[k][j] +
                  (ll)fl[j][a[i]];
          dp[i][j] = min((ll)dp[i][j], res);
        }
      }
    }
    int ans = inf;
    for (int i = 1; i <= n; i++) {
      ans = min(ans, dp[r][i]);
    }
    printf("%d\n", ans);
  }
  return 0;
}