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

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

typedef pair<int, int> ii;
const int MAX_V = 101;
const int IINF = INT_MAX;
struct Edge {
  int dst, weight, hour, comp;
};
struct Data {
  int cur, weight, hour, bitmask;
  bool operator<(const Data &data) const {
    if (weight != data.weight)
      return weight > data.weight;
    return hour > data.hour;
  }
};
int V, E, H, K, S, T, P;
vector<Edge> G[MAX_V], G2[MAX_V];
int mindist[MAX_V][27], dp[1 << 8];
;
ii pass[300];

void set_up() {
  rep(i, (1 << K)) dp[i] = IINF;
  dp[0] = 0;
  rep(i, (1 << K)) if (dp[i] != IINF) {
    rep(j, P) {
      int bit = i | pass[j].second;
      assert(bit < (1 << K));
      dp[bit] = min(dp[bit], dp[i] + pass[j].first);
    }
  }
}

int dijkstra(int bit) {
  rep(i, V) G2[i].clear();
  rep(i, V) {
    rep(j, G[i].size()) {
      Edge edge = G[i][j];
      if ((bit >> edge.comp) & 1)
        edge.weight = 0;
      G2[i].push_back(edge);
    }
  }

  priority_queue<Data> Q;
  rep(i, V) rep(k, H + 1) mindist[i][k] = IINF;
  Q.push((Data){S, dp[bit], 0, 0});
  mindist[S][0] = 0;
  while (!Q.empty()) {
    Data data = Q.top();
    Q.pop();
    if (data.cur == T)
      return data.weight;
    rep(i, G2[data.cur].size()) {
      int next = G2[data.cur][i].dst;
      int nhour = data.hour + G2[data.cur][i].hour;
      if (nhour > H)
        continue;
      if (mindist[next][nhour] > data.weight + G2[data.cur][i].weight) {
        mindist[next][nhour] = data.weight + G2[data.cur][i].weight;
        Q.push((Data){next, mindist[next][nhour], nhour, data.bitmask});
      }
    }
  }
  return -1;
}

void compute() {
  set_up();
  int mini = IINF;
  rep(i, (1 << K)) if (dp[i] != IINF) mini = min(mini, dijkstra(i));
  cout << mini << endl;
}

int main() {
  while (cin >> V >> E >> H >> K, V | E | H | K) {

    rep(i, V) { G[i].clear(); }

    int a, b, c, h, r;
    rep(i, E) {
      cin >> a >> b >> c >> h >> r;
      --a, --b, --r;
      G[a].push_back((Edge){b, c, h, r});
      G[b].push_back((Edge){a, c, h, r});
    }

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

    cin >> P;

    int l, d, k;
    rep(i, P) {
      cin >> l >> d;
      pass[i].first = d;
      pass[i].second = 0;
      rep(j, l) {
        cin >> k;
        assert(1 <= k && k <= K);
        --k;
        pass[i].second |= (1 << k);
      }
    }

    compute();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

typedef pair<int, int> ii;
const int MAX_V = 101;
const int IINF = INT_MAX;
struct Edge {
  int dst, weight, hour, comp;
};
struct Data {
  int cur, weight, hour, bitmask;
  bool operator<(const Data &data) const {
    if (weight != data.weight)
      return weight > data.weight;
    return hour > data.hour;
  }
};
int V, E, H, K, S, T, P;
vector<Edge> G[MAX_V], G2[MAX_V];
int mindist[MAX_V][27], dp[1 << 8];
ii pass[MAX_V];

void set_up() {
  rep(i, (1 << K)) dp[i] = IINF;
  dp[0] = 0;
  rep(i, (1 << K)) if (dp[i] != IINF) {
    rep(j, P) {
      int bit = i | pass[j].second;
      dp[bit] = min(dp[bit], dp[i] + pass[j].first);
    }
  }
}

int dijkstra(int bit) {
  rep(i, V) G2[i].clear();
  rep(i, V) {
    rep(j, G[i].size()) {
      Edge edge = G[i][j];
      if ((bit >> edge.comp) & 1)
        edge.weight = 0;
      G2[i].push_back(edge);
    }
  }

  priority_queue<Data> Q;
  rep(i, V) rep(k, H + 1) mindist[i][k] = IINF;
  Q.push((Data){S, dp[bit], 0, 0});
  mindist[S][0] = 0;
  while (!Q.empty()) {
    Data data = Q.top();
    Q.pop();
    if (data.cur == T)
      return data.weight;
    rep(i, G2[data.cur].size()) {
      int next = G2[data.cur][i].dst;
      int nhour = data.hour + G2[data.cur][i].hour;
      if (nhour > H)
        continue;
      if (mindist[next][nhour] > data.weight + G2[data.cur][i].weight) {
        mindist[next][nhour] = data.weight + G2[data.cur][i].weight;
        Q.push((Data){next, mindist[next][nhour], nhour, data.bitmask});
      }
    }
  }
  return -1;
}

void compute() {
  set_up();
  int mini = IINF;
  rep(i, (1 << K)) if (dp[i] != IINF) mini = min(mini, dijkstra(i));
  cout << mini << endl;
}

int main() {
  while (cin >> V >> E >> H >> K, V | E | H | K) {

    rep(i, V) { G[i].clear(); }

    int a, b, c, h, r;
    rep(i, E) {
      cin >> a >> b >> c >> h >> r;
      --a, --b, --r;
      G[a].push_back((Edge){b, c, h, r});
      G[b].push_back((Edge){a, c, h, r});
    }

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

    cin >> P;

    int l, d, k;
    rep(i, P) {

      cin >> l >> d;
      assert(1 <= l && l <= K);
      assert(1 <= d && d <= 10000);
      pass[i].first = d;
      pass[i].second = 0;
      rep(j, l) {
        cin >> k;
        assert(1 <= k && k <= K);
        --k;
        pass[i].second |= (1 << k);
      }
    }

    compute();
  }
  return 0;
}
/*
fewafawfewpaojfweapjfeaw
fewjapofjweapofjapwoe
mounannann
memori
weafjpoewjfapowej
aaamou
afoewpajfowaejfowpaefwea
eeyannke
fajwepofjwaopjefowpaefjafew
memoriagaaa

ewfjaopfewjafopwe
awfeafaewaf

mofewjapfjeow
fweopafjwoaf
nawefiaopn
nan

nanande
aaafepowjafopawejfwepaofe
*/