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

#include <bits/stdc++.h>
#define f first
#define s second
#define INF 1e9
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
vector<PP> G[200];
int n, m, H, com, S, T;
priority_queue<PP, vector<PP>, greater<PP>> Q;

int dijkstra(int b) {
  while (!Q.empty())
    Q.pop();
  int D[25][200];
  for (int i = 0; i <= H; i++)
    for (int j = 0; j <= n; j++)
      D[i][j] = INF;
  Q.push(PP(P(0, 0), P(S, -1)));
  D[0][0] = 0;
  while (!Q.empty()) {
    PP t = Q.top();
    Q.pop();
    int cost = t.f.f, h = t.f.s, pos = t.s.f;
    if (pos == T)
      return cost;
    if (D[h][pos] < cost)
      continue;

    for (int i = 0; i < (int)G[pos].size(); i++) {
      int ncost = cost + G[pos][i].f.s;
      int nh = h + G[pos][i].s.f;
      int nx = G[pos][i].f.f;
      if ((b & (1 << G[pos][i].s.s)))
        ncost = cost;
      if (nh > H)
        continue;
      if (D[nh][nx] > ncost)
        Q.push(PP(P(ncost, nh), P(nx, -1)));
      D[nh][nx] = min(D[nh][nx], ncost);
    }
  }
  return INF;
}

int main() {
  while (1) {
    cin >> n >> m >> H >> com;
    if (!n && !m && !H && !com)
      break;

    for (int i = 0; i < 200; i++)
      G[i].clear();

    for (int i = 0, a, b, c, h, r; i < m; i++) {
      cin >> a >> b >> c >> h >> r;
      a--, b--, r--;
      G[a].push_back(PP(P(b, c), P(h, r)));
      G[b].push_back(PP(P(a, c), P(h, r)));
    }

    cin >> S >> T;
    S--, T--;
    int pass;

    P pc[300];
    cin >> pass;
    for (int i = 0; i < pass; i++) {
      int j;
      pc[i].s = 0;
      cin >> j >> pc[i].f;
      while (j--) {
        int a;
        cin >> a;
        pc[i].s |= (1 << (a - 1));
      }
    }

    int dp[100000];
    for (int i = 0; i < 300; i++)
      dp[i] = INF;
    dp[0] = 0;

    for (int i = 0; i < (1 << com); i++)
      for (int j = 0; j < pass; j++) {
        if (dp[i] >= INF)
          continue;
        int b = i | pc[j].s, ncost = dp[i] + pc[j].f;
        dp[b] = min(dp[b], ncost);
      }

    int ans = INF;
    for (int i = 0; i < (1 << com); i++) {
      if (dp[i] < INF)
        ans = min(ans, dp[i] + dijkstra(i));
    }

    if (ans >= INF)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
#define INF 1e9
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
vector<PP> G[200];
int n, m, H, com, S, T;
priority_queue<PP, vector<PP>, greater<PP>> Q;

int dijkstra(int b) {
  while (!Q.empty())
    Q.pop();
  int D[25][200];
  for (int i = 0; i <= H; i++)
    for (int j = 0; j <= n; j++)
      D[i][j] = INF;
  Q.push(PP(P(0, 0), P(S, -1)));
  D[0][0] = 0;
  while (!Q.empty()) {
    PP t = Q.top();
    Q.pop();
    int cost = t.f.f, h = t.f.s, pos = t.s.f;
    if (pos == T)
      return cost;
    if (D[h][pos] < cost)
      continue;

    for (int i = 0; i < (int)G[pos].size(); i++) {
      int ncost = cost + G[pos][i].f.s;
      int nh = h + G[pos][i].s.f;
      int nx = G[pos][i].f.f;
      if ((b & (1 << G[pos][i].s.s)))
        ncost = cost;
      if (nh > H)
        continue;
      if (D[nh][nx] > ncost)
        Q.push(PP(P(ncost, nh), P(nx, -1)));
      D[nh][nx] = min(D[nh][nx], ncost);
    }
  }
  return INF;
}

int main() {
  while (1) {
    cin >> n >> m >> H >> com;
    if (!n && !m && !H && !com)
      break;

    for (int i = 0; i < 200; i++)
      G[i].clear();

    for (int i = 0, a, b, c, h, r; i < m; i++) {
      cin >> a >> b >> c >> h >> r;
      a--, b--, r--;
      G[a].push_back(PP(P(b, c), P(h, r)));
      G[b].push_back(PP(P(a, c), P(h, r)));
    }

    cin >> S >> T;
    S--, T--;
    int pass;
    P pc[300];
    cin >> pass;

    for (int i = 0; i < pass; i++) {
      int j;
      pc[i].s = 0;
      cin >> j >> pc[i].f;
      while (j--) {
        int a;
        cin >> a;
        pc[i].s |= (1 << (a - 1));
      }
    }

    int dp[300];
    for (int i = 0; i < 300; i++)
      dp[i] = INF;
    dp[0] = 0;

    for (int i = 0; i < (1 << com); i++)
      for (int j = 0; j < m; j++) {
        if (dp[i] >= INF)
          continue;
        int b = i | pc[j].s, ncost = dp[i] + pc[j].f;
        dp[b] = min(dp[b], ncost);
      }

    int ans = INF;
    for (int i = 0; i < (1 << com); i++) {
      if (dp[i] < INF)
        ans = min(ans, dp[i] + dijkstra(i));
    }

    if (ans >= INF)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}