Problem p01317 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
const int INF = 1 << 22;
int N, M, R;
void wf(VVI &adj) {
  for (int k = 0; k < N; ++k) {
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
      }
    }
  }
}
void print_adj(VVI adj) {
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (adj[i][j] >= INF) {
        cout << "INF"
             << " ";
      } else {
        cout << adj[i][j] << " ";
      }
    }
    cout << endl;
  }
}
void db_print(int line[]) {
  for (int i = 0; i < N; ++i) {
    if (line[i] >= INF) {
      cout << "INF"
           << " ";
    } else {
      cout << line[i] << " ";
    }
  }
  cout << endl;
}
int solve(VVI sea, VVI land, VI que) {
  int table[R][N];
  fill_n((int *)table, (R + 1) * N, INF);
  table[0][que[0]] = 0;
  for (int r = 1; r < R; ++r) {
    // db_print(table[r-1]);
    for (int from = 0; from < N; ++from) {
      for (int to = 0; to < N; ++to) {
        if (from == to) {
          int cost = table[r - 1][from];
          cost += land[que[r - 1]][que[r]];
          table[r][to] = min(table[r][to], cost);
        } else {
          int cost = table[r - 1][from];
          cost += land[que[r - 1]][from];
          cost += sea[from][to];
          cost += land[to][que[r]];
          table[r][to] = min(table[r][to], cost);
        }
      }
    }
    for (int i = 0; i < N; ++i) {
      if (table[r][i] > INF) {
        table[r][i] = INF;
      }
    }
  }
  int res = INF;
  // db_print(table[R-1]);
  for (int i = 0; i < N; ++i) {
    res = min(res, table[R - 1][i]);
  }
  return res;
}
int main() {
  while (true) {
    cin >> N >> M;
    if ((N | M) == 0) {
      break;
    }
    VVI sea(N, vector<int>(N, INF));
    VVI land(N, vector<int>(N, INF));
    for (int i = 0; i < N; ++i) {
      sea[i][i] = 0;
      land[i][i] = 0;
    }
    for (int i = 0; i < M; ++i) {
      int x, y, t;
      char sl;
      cin >> x >> y >> t >> sl;
      --x;
      --y;
      if (sl == 'S') {
        sea[x][y] = sea[y][x] = min(sea[y][x], t);
      } else {
        land[x][y] = land[y][x] = min(land[y][x], t);
      }
    }
    wf(sea);
    wf(land);
    // print_adj(sea);
    // print_adj(land);
    cin >> R;
    vector<int> que(R);
    for (int i = 0; i < R; ++i) {
      cin >> que[i];
      --que[i];
    }
    cout << solve(sea, land, que) << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
const int INF = 1 << 22;
int N, M, R;
void wf(VVI &adj) {
  for (int k = 0; k < N; ++k) {
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
      }
    }
  }
}
void print_adj(VVI adj) {
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (adj[i][j] >= INF) {
        cout << "INF"
             << " ";
      } else {
        cout << adj[i][j] << " ";
      }
    }
    cout << endl;
  }
}
void db_print(int line[]) {
  for (int i = 0; i < N; ++i) {
    if (line[i] >= INF) {
      cout << "INF"
           << " ";
    } else {
      cout << line[i] << " ";
    }
  }
  cout << endl;
}
int solve(VVI sea, VVI land, VI que) {
  int table[R][N];
  fill_n((int *)table, R * N, INF);
  table[0][que[0]] = 0;
  for (int r = 1; r < R; ++r) {
    // db_print(table[r-1]);
    for (int from = 0; from < N; ++from) {
      for (int to = 0; to < N; ++to) {
        if (from == to) {
          int cost = table[r - 1][from];
          cost += land[que[r - 1]][que[r]];
          table[r][to] = min(table[r][to], cost);
        } else {
          int cost = table[r - 1][from];
          cost += land[que[r - 1]][from];
          cost += sea[from][to];
          cost += land[to][que[r]];
          table[r][to] = min(table[r][to], cost);
        }
      }
    }
    for (int i = 0; i < N; ++i) {
      if (table[r][i] > INF) {
        table[r][i] = INF;
      }
    }
  }
  int res = INF;
  // db_print(table[R-1]);
  for (int i = 0; i < N; ++i) {
    res = min(res, table[R - 1][i]);
  }
  return res;
}
int main() {
  while (true) {
    cin >> N >> M;
    if ((N | M) == 0) {
      break;
    }
    VVI sea(N, vector<int>(N, INF));
    VVI land(N, vector<int>(N, INF));
    for (int i = 0; i < N; ++i) {
      sea[i][i] = 0;
      land[i][i] = 0;
    }
    for (int i = 0; i < M; ++i) {
      int x, y, t;
      char sl;
      cin >> x >> y >> t >> sl;
      --x;
      --y;
      if (sl == 'S') {
        sea[x][y] = sea[y][x] = min(sea[y][x], t);
      } else {
        land[x][y] = land[y][x] = min(land[y][x], t);
      }
    }
    wf(sea);
    wf(land);
    // print_adj(sea);
    // print_adj(land);
    cin >> R;
    vector<int> que(R);
    for (int i = 0; i < R; ++i) {
      cin >> que[i];
      --que[i];
    }
    cout << solve(sea, land, que) << endl;
  }
  return 0;
}