Problem p01697 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define N_MAX 100
#define INF (1e9)
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> P1;
typedef pair<int, P> P2;
int a, b, c, h, r, s, t, p, l, d, k, ans;
int N, M, H, K, dp[300][(1 << 8)];
vector<P1> v[N_MAX];
P L[300];

void dijkstra(int S, int C) {
  int d[25][N_MAX];
  priority_queue<P2> q;
  for (int i = 0; i <= H; i++)
    for (int j = 0; j < N; j++)
      d[i][j] = INF;
  d[0][s - 1] = 0;
  q.push(P2(0, P(0, s - 1)));
  while (!q.empty()) {
    P2 A = q.top();
    q.pop();
    int cost = A.first;
    int tim = A.second.first;
    int x = A.second.second;
    if (d[tim][x] < cost)
      continue;
    for (int i = 0; i < v[x].size(); i++) {
      int nx = v[x][i].first.first;
      int pc = v[x][i].first.second;
      int ph = v[x][i].second.first;
      int R = v[x][i].second.second;
      if (tim + ph <= H) {
        if (!(S & (1 << R)) && d[tim + ph][nx] > cost + pc) {
          d[tim + ph][nx] = cost + pc;
          q.push(P2(cost + pc, P(tim + ph, nx)));
        }
        if ((S & (1 << R)) && d[tim + ph][nx] > cost) {
          d[tim + ph][nx] = cost;
          q.push(P2(cost, P(tim + ph, nx)));
        }
      }
    }
  }
  int mind = INF;
  for (int i = 0; i <= H; i++)
    mind = min(mind, C + d[i][t - 1]);
  ans = min(ans, mind);
}

int main() {
  while (1) {
    cin >> N >> M >> H >> K;
    if (!N && !M && !H && !K)
      break;
    for (int i = 0; i < M; i++) {
      cin >> a >> b >> c >> h >> r;
      v[a - 1].push_back(P1(P(b - 1, c), P(h, r - 1)));
      v[b - 1].push_back(P1(P(a - 1, c), P(h, r - 1)));
    }
    cin >> s >> t;
    cin >> p;
    for (int i = 0; i <= p; i++)
      for (int j = 0; j < (1 << K); j++)
        dp[i][j] = INF;
    for (int i = 0; i < p; i++) {
      cin >> l >> d;
      L[i] = P(d, 0);
      for (int j = 0; j < l; j++)
        cin >> k, L[i].second |= (1 << (k - 1));
      dp[i][L[i].second] = d;
    }
    for (int i = 0; i < p; i++) {
      for (int j = 0; j < (1 << K); j++) {
        if (dp[i][j] == INF)
          continue;
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
        dp[i + 1][j | L[i].second] =
            min(dp[i + 1][j | L[i].second], dp[i][j] + L[i].first);
      }
    }
    ans = INF;
    dijkstra(0, 0);
    for (int i = 0; i < (1 << K); i++) {
      if (dp[p][i] == INF)
        continue;
      dijkstra(i, dp[p][i]);
    }
    if (ans != INF)
      cout << ans << endl;
    else
      cout << -1 << endl;
    for (int i = 0; i < N; i++)
      v[i].clear();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N_MAX 100
#define INF (1e9)
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> P1;
typedef pair<int, P> P2;
int a, b, c, h, r, s, t, p, l, d, k, ans;
int N, M, H, K, dp[300][(1 << 8)];
vector<P1> v[N_MAX];
P L[300];

void dijkstra(int S, int C) {
  int d[25][N_MAX];
  priority_queue<P2, vector<P2>, greater<P2>> q;
  for (int i = 0; i <= H; i++)
    for (int j = 0; j < N; j++)
      d[i][j] = INF;
  d[0][s - 1] = 0;
  q.push(P2(0, P(0, s - 1)));
  while (!q.empty()) {
    P2 A = q.top();
    q.pop();
    int cost = A.first;
    int tim = A.second.first;
    int x = A.second.second;
    if (d[tim][x] < cost)
      continue;
    for (int i = 0; i < v[x].size(); i++) {
      int nx = v[x][i].first.first;
      int pc = v[x][i].first.second;
      int ph = v[x][i].second.first;
      int R = v[x][i].second.second;
      if (tim + ph <= H) {
        if (!(S & (1 << R)) && d[tim + ph][nx] > cost + pc) {
          d[tim + ph][nx] = cost + pc;
          q.push(P2(cost + pc, P(tim + ph, nx)));
        }
        if ((S & (1 << R)) && d[tim + ph][nx] > cost) {
          d[tim + ph][nx] = cost;
          q.push(P2(cost, P(tim + ph, nx)));
        }
      }
    }
  }
  int mind = INF;
  for (int i = 0; i <= H; i++)
    mind = min(mind, C + d[i][t - 1]);
  ans = min(ans, mind);
}

int main() {
  while (1) {
    cin >> N >> M >> H >> K;
    if (!N && !M && !H && !K)
      break;
    for (int i = 0; i < M; i++) {
      cin >> a >> b >> c >> h >> r;
      v[a - 1].push_back(P1(P(b - 1, c), P(h, r - 1)));
      v[b - 1].push_back(P1(P(a - 1, c), P(h, r - 1)));
    }
    cin >> s >> t;
    cin >> p;
    for (int i = 0; i <= p; i++)
      for (int j = 0; j < (1 << K); j++)
        dp[i][j] = INF;
    for (int i = 0; i < p; i++) {
      cin >> l >> d;
      L[i] = P(d, 0);
      for (int j = 0; j < l; j++)
        cin >> k, L[i].second |= (1 << (k - 1));
      dp[i][L[i].second] = d;
    }
    for (int i = 0; i < p; i++) {
      for (int j = 0; j < (1 << K); j++) {
        if (dp[i][j] == INF)
          continue;
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
        dp[i + 1][j | L[i].second] =
            min(dp[i + 1][j | L[i].second], dp[i][j] + L[i].first);
      }
    }
    ans = INF;
    dijkstra(0, 0);
    for (int i = 0; i < (1 << K); i++) {
      if (dp[p][i] == INF)
        continue;
      dijkstra(i, dp[p][i]);
    }
    if (ans != INF)
      cout << ans << endl;
    else
      cout << -1 << endl;
    for (int i = 0; i < N; i++)
      v[i].clear();
  }
  return 0;
}