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

#include <bits/stdc++.h>
#define INF (1LL << 50)
#define s second
#define f first
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
ll G[200][200][2];
int n, m, r, z[1000];

void WF() {
  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        G[i][j][0] = min(G[i][j][0], G[i][k][0] + G[k][j][0]);
        G[i][j][1] = min(G[i][j][1], G[i][k][1] + G[k][j][1]);
      }
}

ll dijkstra() {
  vector<vector<ll>> D(n, vector<ll>(r, INF));
  vector<vector<int>> used(n, vector<int>(r, 0));
  priority_queue<PP, vector<PP>, greater<PP>> Q;
  Q.push(PP(0, P(z[0], 0)));
  D[z[0]][0] = 0;

  while (!Q.empty()) {
    PP t = Q.top();
    Q.pop();
    ll cost = t.f, ship = t.s.f, idx = t.s.s, pos = z[idx];
    if (idx == r - 1)
      return cost;
    if (used[ship][idx]++)
      continue;

    int npos = z[idx + 1];
    ll ncost = cost + G[pos][npos][0];
    if (D[ship][idx + 1] > ncost)
      Q.push(PP(ncost, P(ship, idx + 1))), D[ship][idx + 1] = ncost;

    for (int i = 0; i < n; i++) {
      ncost = cost + G[pos][ship][0] + G[ship][i][1] + G[i][npos][0];
      if (D[i][idx + 1] > ncost)
        Q.push(PP(ncost, P(i, idx + 1))), D[i][idx + 1] = ncost;
    }
  }
  assert(0);
}

int main() {
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        G[i][j][0] = G[i][j][1] = INF * (i != j);

    for (int i = 0, x, y, c, idx; i < m; i++) {
      char ch;
      cin >> x >> y >> c >> ch;
      x--, y--;
      idx = (ch == 'S');
      G[x][y][idx] = G[y][x][idx] = min(G[x][y][idx], (ll)c);
    }
    cin >> r;
    for (int i = 0; i < r; i++)
      cin >> z[i], z[i]--;
    WF();
    cout << dijkstra() << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define INF (1LL << 50)
#define s second
#define f first
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
ll G[200][200][2];
int n, m, r, z[1000];

void WF() {
  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        G[i][j][0] = min(G[i][j][0], G[i][k][0] + G[k][j][0]);
        G[i][j][1] = min(G[i][j][1], G[i][k][1] + G[k][j][1]);
      }
}

int dijkstra() {
  vector<vector<ll>> D(n, vector<ll>(r, INF));
  vector<vector<int>> used(n, vector<int>(r, 0));
  priority_queue<PP, vector<PP>, greater<PP>> Q;
  Q.push(PP(0, P(z[0], 0)));
  D[z[0]][0] = 0;

  while (!Q.empty()) {
    PP t = Q.top();
    Q.pop();
    ll cost = t.f, ship = t.s.f, idx = t.s.s, pos = z[idx];
    if (cost < 0)
      assert(0);
    if (idx == r - 1)
      return cost;
    if (used[ship][idx]++)
      continue;

    int npos = z[idx + 1];
    ll ncost = cost + G[pos][npos][0];
    if (D[ship][idx + 1] > ncost)
      Q.push(PP(ncost, P(ship, idx + 1)));
    if (pos == ship)
      for (int i = 0; i < n; i++) {
        ncost = cost + G[pos][i][1] + G[i][npos][0];
        if (D[i][idx + 1] > ncost)
          Q.push(PP(ncost, P(i, idx + 1)));
      }
  }
  assert(0);
}

int main() {
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        G[i][j][0] = G[i][j][1] = INF * (i != j);

    for (int i = 0, x, y, c, idx; i < m; i++) {
      char ch;
      cin >> x >> y >> c >> ch;
      x--, y--;
      idx = (ch == 'S');
      G[x][y][idx] = G[y][x][idx] = min(G[x][y][idx], (ll)c);
    }
    cin >> r;
    for (int i = 0; i < r; i++)
      cin >> z[i], z[i]--;
    WF();
    cout << dijkstra() << endl;
  }
  return 0;
}