Problem p01317 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 114514810;
const int MOD = 1000000007;
const double EPS = 1e-10;

typedef int weight;
struct edge {
  int to;
  weight cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};

typedef vector<edge> Edges;
typedef vector<Edges> Graph;
// int dx[] = { -1, 0, 0, 1 }; int dy[] = { 0, -1, 1, 0 };
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
bool valid(int x, int y, int h, int w) {
  return (x >= 0 && y >= 0 && x < h && y < w);
}
int place(int x, int y, int w) { return w * x + y; }
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int n, m, r;
int dl[201][201], ds[201][201], z[1001];
int dp[1001][201];

int solve(int i, int ship) {
  if (i == r - 1)
    return 0;
  if (dp[i][ship] > 0)
    return dp[i][ship];
  int res = solve(i + 1, ship) + dl[z[i]][z[i + 1]];
  REP(j, n) {
    int sum = dl[z[i]][ship] + ds[ship][j] + dl[j][z[i + 1]];
    chmin(res, solve(i + 1, j) + sum);
  }
  return dp[i][ship] = res;
}

int main() {
  while (cin >> n >> m, n) {
    MS(dp, -1);
    REP(i, n) REP(j, n) {
      dl[i][j] = (i == j ? 0 : INF);
      ds[i][j] = (i == j ? 0 : INF);
    }
    REP(i, m) {
      int x, y, t;
      char c;
      cin >> x >> y >> t >> c;
      x--;
      y--;
      if (c == 'L')
        dl[x][y] = dl[y][x] = t;
      else
        ds[x][y] = ds[y][x] = t;
    }
    REP(k, n) REP(i, n) REP(j, n) {
      chmin(dl[i][j], dl[i][k] + dl[k][j]);
      chmin(ds[i][j], ds[i][k] + ds[k][j]);
    }
    cin >> r;
    REP(i, r) {
      cin >> z[i];
      z[i]--;
    }
    cout << solve(z[0], z[0]) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 114514810;
const int MOD = 1000000007;
const double EPS = 1e-10;

typedef int weight;
struct edge {
  int to;
  weight cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};

typedef vector<edge> Edges;
typedef vector<Edges> Graph;
// int dx[] = { -1, 0, 0, 1 }; int dy[] = { 0, -1, 1, 0 };
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
bool valid(int x, int y, int h, int w) {
  return (x >= 0 && y >= 0 && x < h && y < w);
}
int place(int x, int y, int w) { return w * x + y; }
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int n, m, r;
int dl[201][201], ds[201][201], z[1001];
int dp[1001][201];

int solve(int i, int ship) {
  if (i == r - 1)
    return 0;
  if (dp[i][ship] > 0)
    return dp[i][ship];
  int res = solve(i + 1, ship) + dl[z[i]][z[i + 1]];
  REP(j, n) {
    int sum = dl[z[i]][ship] + ds[ship][j] + dl[j][z[i + 1]];
    chmin(res, solve(i + 1, j) + sum);
  }
  return dp[i][ship] = res;
}

int main() {
  while (cin >> n >> m, n) {
    MS(dp, -1);
    REP(i, n) REP(j, n) {
      dl[i][j] = (i == j ? 0 : INF);
      ds[i][j] = (i == j ? 0 : INF);
    }
    REP(i, m) {
      int x, y, t;
      char c;
      cin >> x >> y >> t >> c;
      x--;
      y--;
      if (c == 'L')
        dl[x][y] = dl[y][x] = t;
      else
        ds[x][y] = ds[y][x] = t;
    }
    REP(k, n) REP(i, n) REP(j, n) {
      chmin(dl[i][j], dl[i][k] + dl[k][j]);
      chmin(ds[i][j], ds[i][k] + ds[k][j]);
    }
    cin >> r;
    REP(i, r) {
      cin >> z[i];
      z[i]--;
    }
    cout << solve(0, z[0]) << endl;
  }
  return 0;
}