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

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> PII;

const int MAX_N = 210;
const int MAX_H = 50;
const int INF = 1e9;

struct edge {
  int to, cost, hour, r;
  edge(int to, int cost, int hour, int r)
      : to(to), cost(cost), hour(hour), r(r) {}
};

struct State {
  int v, hour, cost;
  State(int v, int hour, int cost) : v(v), hour(hour), cost(cost) {}
};

int N, M, H, K;
vector<edge> G[MAX_N];
vector<PII> V;
int S, T, P;
int money[(1 << 12)];
int dp[MAX_H][MAX_N];

void init() {
  for (int i = 0; i < MAX_N; i++)
    G[i].clear();
  V.clear();
  fill(money, money + (1 << 12), INF);
}

int solve(int bit) {
  for (int i = 0; i < MAX_H; i++)
    for (int j = 0; j < MAX_N; j++)
      dp[i][j] = INF;

  queue<State> que;
  que.push(State(S, 0, 0));

  dp[0][S] = 0;

  while (!que.empty()) {
    State q = que.front();
    que.pop();

    int v = q.v;
    for (int i = 0; i < (int)G[v].size(); i++) {

      int r = G[v][i].r;
      int nexv = G[v][i].to;
      int nexh = q.hour + G[v][i].hour;
      int nexc;

      nexc = q.cost + G[v][i].cost;
      if ((bit >> r) & 1)
        nexc = q.cost;

      if (dp[nexh][nexv] > nexc && nexh <= H) {
        dp[nexh][nexv] = nexc;

        que.push(State(nexv, nexh, nexc));
      }
    }
  }
  int ret = INF;
  for (int i = 0; i <= 24; i++)
    ret = min(ret, dp[i][T]);
  return ret;
}

void rec(int bit, int cost) {
  if (money[bit] <= cost)
    return;
  money[bit] = cost;
  for (int i = 0; i < (int)V.size(); i++) {
    int t = bit | V[i].first;
    if (t == bit)
      continue;
    rec(t, cost + V[i].second);
  }
}

int main() {
  while (cin >> N >> M >> H >> K, N | M | H | K) {
    init();

    int from, to, cost, hour, r;
    for (int i = 0; i < M; i++) {
      cin >> from >> to >> cost >> hour >> r;
      from--, to--, r--;
      G[from].push_back(edge(to, cost, hour, r));
      G[to].push_back(edge(from, cost, hour, r));
    }

    cin >> S >> T;
    S--, T--;
    cin >> P;

    for (int i = 0; i < P; i++) {
      int l, d;
      cin >> l >> d;
      int k;
      int bit = 0;
      for (int j = 0; j < l; j++) {
        cin >> k;
        k--;
        bit += (1 << k);
      }
      V.push_back(PII(bit, d));
    }

    rec(0, 0);

    int ans = INF;
    for (int i = 0; i < (1 << K); i++) {
      ans = min(ans, solve(i) + money[i]);
    }

    cout << (ans == INF ? -1 : ans) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> PII;

const int MAX_N = 150;
const int MAX_H = 30;
const int INF = 1e9;

struct edge {
  int to, cost, hour, r;
  edge(int to, int cost, int hour, int r)
      : to(to), cost(cost), hour(hour), r(r) {}
};

struct State {
  int v, hour, cost;
  State(int v, int hour, int cost) : v(v), hour(hour), cost(cost) {}
};

int N, M, H, K;
vector<edge> G[MAX_N];
vector<PII> V;
int S, T, P;
int money[(1 << 10)];
int dp[MAX_H][MAX_N];

void init() {
  for (int i = 0; i < MAX_N; i++)
    G[i].clear();
  V.clear();
  fill(money, money + (1 << 10), INF);
}

int solve(int bit) {
  for (int i = 0; i < MAX_H; i++)
    for (int j = 0; j < MAX_N; j++)
      dp[i][j] = INF;

  queue<State> que;
  que.push(State(S, 0, 0));

  dp[0][S] = 0;

  while (!que.empty()) {
    State q = que.front();
    que.pop();

    int v = q.v;
    for (int i = 0; i < (int)G[v].size(); i++) {

      int r = G[v][i].r;
      int nexv = G[v][i].to;
      int nexh = q.hour + G[v][i].hour;
      int nexc;

      nexc = q.cost + G[v][i].cost;
      if ((bit >> r) & 1)
        nexc = q.cost;

      if (dp[nexh][nexv] > nexc && nexh <= H) {
        dp[nexh][nexv] = nexc;

        que.push(State(nexv, nexh, nexc));
      }
    }
  }
  int ret = INF;
  for (int i = 0; i <= 24; i++)
    ret = min(ret, dp[i][T]);
  return ret;
}

void rec(int bit, int cost) {
  if (money[bit] <= cost)
    return;
  money[bit] = cost;
  for (int i = 0; i < (int)V.size(); i++) {
    int t = bit | V[i].first;
    if (t == bit)
      continue;
    rec(t, cost + V[i].second);
  }
}

int main() {
  while (cin >> N >> M >> H >> K, N | M | H | K) {
    init();

    int from, to, cost, hour, r;
    for (int i = 0; i < M; i++) {
      cin >> from >> to >> cost >> hour >> r;
      from--, to--, r--;
      G[from].push_back(edge(to, cost, hour, r));
      G[to].push_back(edge(from, cost, hour, r));
    }

    cin >> S >> T;
    S--, T--;
    cin >> P;

    for (int i = 0; i < P; i++) {
      int l, d;
      cin >> l >> d;
      int k;
      int bit = 0;
      for (int j = 0; j < l; j++) {
        cin >> k;
        k--;
        bit += (1 << k);
      }
      V.push_back(PII(bit, d));
    }

    rec(0, 0);

    int ans = INF;
    for (int i = 0; i < (1 << K); i++) {
      ans = min(ans, solve(i) + money[i]);
    }

    cout << (ans == INF ? -1 : ans) << endl;
  }

  return 0;
}