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

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

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define repr(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x " = " << x << endl
#define print(x) cout << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef pair<int, P> PIP;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
typedef set<int> S;

#define INF INT_MAX / 3
#define MAX_N 1000000001

int d[8][100][100][2];
int pas[256];
int bit[256];
int visited[100][25];
int cost[1 << 8];
vector<int> v[8][100];

struct State {
  int v, c, t;
};

bool operator<(const State &a, const State &b) {
  if (a.c != b.c)
    return a.c > b.c;
  return a.t > b.t;
}

int main() {
  cin.sync_with_stdio(false);
  int n, m, h, k, s, t, p;
  while (cin >> n >> m >> h >> k, n) {
    memset(d, -1, sizeof(d));
    memset(pas, -1, sizeof(pas));
    // fill(v, v + n, vector<int>());
    fill(cost, cost + (1 << k), INF);
    rep(i, 8) rep(j, 100) v[i][j].clear();

    rep(i, m) {
      int a, b, c, e, r;
      cin >> a >> b >> c >> e >> r;
      a--, b--, r--;
      d[r][a][b][0] = d[r][b][a][0] = c;
      d[r][a][b][1] = d[r][b][a][1] = e;
      v[r][a].pb(b), v[r][b].pb(a);
    }
    cin >> s >> t >> p;
    s--, t--;
    rep(i, p) {
      int l, b = 0;
      cin >> l >> pas[i];
      rep(j, l) {
        int tmp;
        cin >> tmp;
        tmp--;
        b |= (1 << tmp);
      }
      bit[i] = b;
    }

    int mini = INF;
    cost[0] = 0;
    rep(i, 1 << k) {
      rep(j, p) { minch(cost[i | bit[j]], cost[i] + pas[j]); }
    }

    rep(i, 1 << k) {
      if (cost[i] == INF)
        continue;

      rep(j, n) rep(l, h + 1) visited[j][l] = INF;
      priority_queue<State> q;
      q.push(State{s, cost[i], 0});

      int ans = INF;
      while (!q.empty()) {
        State st = q.top();
        q.pop();

        if (visited[st.v][st.t] != INF || st.t > h)
          continue;
        else
          visited[st.v][st.t] = st.c;

        bool ok = true;
        rep(j, h + 1) if (visited[t][j] == INF) ok = false;
        if (ok)
          break;

        rep(l, k) rep(w, v[l][st.v].size()) {
          int j = v[l][st.v][w];
          if (d[l][st.v][j][0] > 0 && d[l][st.v][j][1] + st.t <= h) {
            if (visited[j][st.t + d[l][st.v][j][1]] != INF)
              continue;
            if (i & (1 << l)) {
              q.push(State{j, st.c, st.t + d[l][st.v][j][1]});
            } else {
              q.push(
                  State{j, st.c + d[l][st.v][j][0], st.t + d[l][st.v][j][1]});
            }
          }
        }
      }

      rep(i, h + 1) minch(mini, visited[t][i]);
    }
    if (mini < INF)
      cout << mini << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define repr(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x " = " << x << endl
#define print(x) cout << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef pair<int, P> PIP;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
typedef set<int> S;

#define INF INT_MAX / 3
#define MAX_N 1000000001

int d[8][100][100][2];
int pas[256];
int bit[256];
int visited[100][25];
int cost[1 << 8];
vector<int> v[8];

struct State {
  int v, c, t;
};

bool operator<(const State &a, const State &b) {
  if (a.c != b.c)
    return a.c > b.c;
  return a.t > b.t;
}

int main() {
  cin.sync_with_stdio(false);
  int n, m, h, k, s, t, p;
  while (cin >> n >> m >> h >> k, n) {
    memset(d, -1, sizeof(d));
    memset(pas, -1, sizeof(pas));
    fill(v, v + n, vector<int>());
    fill(cost, cost + (1 << k), INF);

    rep(i, m) {
      int a, b, c, e, r;
      cin >> a >> b >> c >> e >> r;
      a--, b--, r--;
      d[r][a][b][0] = d[r][b][a][0] = c;
      d[r][a][b][1] = d[r][b][a][1] = e;
      v[a].pb(b), v[b].pb(a);
    }
    cin >> s >> t >> p;
    s--, t--;
    rep(i, p) {
      int l, b = 0;
      cin >> l >> pas[i];
      rep(j, l) {
        int tmp;
        cin >> tmp;
        tmp--;
        b |= (1 << tmp);
      }
      bit[i] = b;
    }

    int mini = INF;
    cost[0] = 0;
    rep(i, 1 << k) {
      rep(j, p) { minch(cost[i | bit[j]], cost[i] + pas[j]); }
    }

    rep(i, 1 << k) {
      if (cost[i] == INF)
        continue;

      rep(i, n) rep(j, h + 1) visited[i][j] = INF;
      priority_queue<State> q;
      q.push(State{s, cost[i], 0});

      int ans = INF;
      while (!q.empty()) {
        State st = q.top();
        q.pop();

        if (visited[st.v][st.t] != INF || st.t > h)
          continue;
        else
          visited[st.v][st.t] = st.c;

        bool ok = true;
        rep(i, h + 1) if (visited[t][i] == INF) ok = false;
        if (ok)
          break;

        rep(l, k) rep(w, v[st.v].size()) {
          int j = v[st.v][w];
          if (d[l][st.v][j][0] > 0 && d[l][st.v][j][1] + st.t <= h) {
            if (visited[j][st.t + d[l][st.v][j][1]] != INF)
              continue;
            if (i & (1 << l)) {
              q.push(State{j, st.c, st.t + d[l][st.v][j][1]});
            } else {
              q.push(
                  State{j, st.c + d[l][st.v][j][0], st.t + d[l][st.v][j][1]});
            }
          }
        }
      }

      rep(i, h + 1) minch(mini, visited[t][i]);
    }
    if (mini < INF)
      cout << mini << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}