Problem p01697 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using pi = tuple<int, int, int>;
const int INF = 100000000;

int main() {
  int N, M, H, K, S, T, P;
  while (cin >> N >> M >> H >> K, N | M | H | K) {
    vector<int> a(M), b(M), c(M), h(M), r(M);
    for (int i = 0; i < M; i++) {
      cin >> a[i] >> b[i] >> c[i] >> h[i] >> r[i];
      a[i]--;
      b[i]--;
      r[i]--;
    }
    cin >> S >> T >> P;
    S--;
    T--;
    vector<int> dp(1 << K, INF);
    dp[0] = 0;
    for (int i = 0, l, d; i < P; i++) {
      cin >> l >> d;
      int val = 0;
      for (int j = 0, k; j < l; j++) {
        cin >> k;
        val += 1 << (k - 1);
      }
      for (int j = 0; j < (1 << K); j++) {
        if (dp[j] != INF) {
          dp[j | val] = min(dp[j | val], dp[j] + d);
        }
      }
    }
    int res = INF;
    for (int i = 0; i < (1 << K); i++) {
      if (dp[i] == INF)
        continue;
      vector<vector<int>> d(N, vector<int>(H + 1, INF));
      vector<vector<pi>> G(N);
      for (int j = 0; j < M; j++) {
        if (i & (1 << r[j])) {
          G[a[j]].push_back(pi(0, h[j], b[j]));
          G[b[j]].push_back(pi(0, h[j], a[j]));
        } else {
          G[a[j]].push_back(pi(c[j], h[j], b[j]));
          G[b[j]].push_back(pi(c[j], h[j], a[j]));
        }
      }
      priority_queue<pi, vector<pi>, greater<pi>> pq;
      pq.push(pi(0, 0, S));
      while (!pq.empty()) {
        auto p = pq.top();
        pq.pop();
        if (d[get<2>(p)][get<1>(p)] != INF) {
          continue;
        }
        d[get<2>(p)][get<1>(p)] = get<0>(p);
        for (auto v : G[get<2>(p)]) {
          if (get<1>(p) + get<1>(v) <= H &&
              d[get<2>(v)][get<1>(p) + get<1>(v)] == INF) {
            pq.push(
                pi(get<0>(p) + get<0>(v), get<1>(p) + get<1>(v), get<2>(v)));
          }
        }
      }
      int mid = INF;
      for (int i = 0; i <= H; i++) {
        mid = min(mid, d[T][i]);
      }
      res = min(res, dp[i] + mid);
    }
    cout << (res != INF ? res : -1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using pi = tuple<int, int, int>;
const int INF = 100000000;

int main() {
  int N, M, H, K, S, T, P;
  while (cin >> N >> M >> H >> K, N | M | H | K) {
    vector<int> a(M), b(M), c(M), h(M), r(M);
    for (int i = 0; i < M; i++) {
      cin >> a[i] >> b[i] >> c[i] >> h[i] >> r[i];
      a[i]--;
      b[i]--;
      r[i]--;
    }
    cin >> S >> T >> P;
    S--;
    T--;
    vector<int> dp(1 << K, INF);
    dp[0] = 0;
    for (int i = 0, l, d; i < P; i++) {
      cin >> l >> d;
      int val = 0;
      for (int j = 0, k; j < l; j++) {
        cin >> k;
        val += 1 << (k - 1);
      }
      for (int j = 0; j < (1 << K); j++) {
        if (dp[j] != INF) {
          dp[j | val] = min(dp[j | val], dp[j] + d);
        }
      }
    }
    int res = INF;
    for (int i = 0; i < (1 << K); i++) {
      if (dp[i] == INF)
        continue;
      vector<vector<int>> d(N, vector<int>(H + 1, INF));
      vector<vector<pi>> G(N);
      for (int j = 0; j < M; j++) {
        if (i & (1 << r[j])) {
          G[a[j]].push_back(pi(0, h[j], b[j]));
          G[b[j]].push_back(pi(0, h[j], a[j]));
        } else {
          G[a[j]].push_back(pi(c[j], h[j], b[j]));
          G[b[j]].push_back(pi(c[j], h[j], a[j]));
        }
      }
      priority_queue<pi, vector<pi>, greater<pi>> pq;
      pq.push(pi(0, 0, S));
      while (!pq.empty()) {
        auto p = pq.top();
        pq.pop();
        if (d[get<2>(p)][get<1>(p)] != INF) {
          continue;
        }
        d[get<2>(p)][get<1>(p)] = get<0>(p);
        for (auto v : G[get<2>(p)]) {
          if (get<1>(p) + get<1>(v) <= H) {
            pq.push(
                pi(get<0>(p) + get<0>(v), get<1>(p) + get<1>(v), get<2>(v)));
          }
        }
      }
      int mid = INF;
      for (int i = 0; i <= H; i++) {
        mid = min(mid, d[T][i]);
      }
      res = min(res, dp[i] + mid);
    }
    cout << (res != INF ? res : -1) << endl;
  }
  return 0;
}