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

#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define inf 1e8

using namespace std;

int n, m, s[202][202], h[202][202], dp[1024][202], l[1024];

int main() {
  int i, j, k, x, y, z;
  char c[2];
  while (scanf("%d%d", &n, &m) && (n || m)) {
    for (i = 1; i <= n; i++) {
      fill(h[i], h[i] + n + 1, inf);
      fill(s[i], s[i] + n + 1, inf);
      fill(dp[i], dp[i] + n + 1, inf);
      h[i][i] = s[i][i] = 0;
    }
    for (i = 0; i < m; i++) {
      scanf("%d%d%d%s", &x, &y, &z, c);
      if (c[0] == 'L') {
        h[y][x] = min(h[y][x], z);
        h[x][y] = h[y][x];
      } else {
        s[y][x] = min(s[y][x], z);
        s[x][y] = s[y][x];
      }
    }
    for (k = 1; k <= n; k++)
      for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++) {
          h[i][j] = min(h[i][j], h[i][k] + h[k][j]);
          s[i][j] = min(s[i][j], s[i][k] + s[k][j]);
        }
    scanf("%d", &m);
    for (i = 0; i < m; i++)
      scanf("%d", &l[i]);
    for (j = 1; j <= n; j++)
      dp[0][j] = s[l[0]][j] + h[j][l[0]];
    for (i = n; i <= m; i++)
      fill(dp[i], dp[i] + n + 1, inf);
    for (i = 1; i < m; i++) {
      for (j = 1; j <= n; j++) {
        for (k = 1; k <= n; k++) {
          if (j != k)
            dp[i][k] = min(dp[i][k], dp[i - 1][j] + h[l[i - 1]][j] + s[j][k] +
                                         h[k][l[i]]);
          else
            dp[i][k] = min(dp[i][k], dp[i - 1][k] + h[l[i - 1]][l[i]]);
        }
      }
    }
    printf("%d\\n", *min_element(dp[m - 1] + 1, dp[m - 1] + n + 1));
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define inf 1e8

using namespace std;

int n, m, s[202][202], h[202][202], dp[202][202], l[202];

int main() {
  int i, j, k, x, y, z;
  char c[2];
  while (scanf("%d%d", &n, &m) && (n || m)) {
    for (i = 1; i <= n; i++) {
      fill(h[i], h[i] + n + 1, inf);
      fill(s[i], s[i] + n + 1, inf);
      fill(dp[i], dp[i] + n + 1, inf);
      h[i][i] = s[i][i] = 0;
    }
    for (i = 0; i < m; i++) {
      scanf("%d%d%d%s", &x, &y, &z, c);
      if (c[0] == 'L') {
        h[y][x] = min(h[y][x], z);
        h[x][y] = h[y][x];
      } else {
        s[y][x] = min(s[y][x], z);
        s[x][y] = s[y][x];
      }
    }
    for (k = 1; k <= n; k++)
      for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++) {
          h[i][j] = min(h[i][j], h[i][k] + h[k][j]);
          s[i][j] = min(s[i][j], s[i][k] + s[k][j]);
        }
    scanf("%d", &m);
    for (i = 0; i < m; i++)
      scanf("%d", &l[i]);
    for (j = 1; j <= n; j++)
      dp[0][j] = s[l[0]][j] + h[j][l[0]];
    for (i = 1; i < m; i++) {
      for (j = 1; j <= n; j++) {
        for (k = 1; k <= n; k++) {
          if (j != k)
            dp[i][k] = min(dp[i][k], dp[i - 1][j] + h[l[i - 1]][j] + s[j][k] +
                                         h[k][l[i]]);
          else
            dp[i][k] = min(dp[i][k], dp[i - 1][k] + h[l[i - 1]][l[i]]);
        }
      }
    }
    printf("%d\\n", *min_element(dp[m - 1] + 1, dp[m - 1] + n + 1));
  }
  return 0;
}