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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX_N 111
#define INF 1 << 25
typedef pair<int, int> P;
typedef pair<int, P> PP;
#define F first
#define S second
struct edge {
  int to, cost, hour, com;
  edge() {}
  edge(int to, int cost, int hour, int com)
      : to(to), cost(cost), hour(hour), com(com) {}
};
struct pass {
  int bit, cost;
  pass() { bit = 0; }
  pass(int bit, int cost) : bit(bit), cost(cost) {}
};
vector<edge> G[MAX_N];
int main() {
  int n, m, h, k;
  while (cin >> n >> m >> h >> k, n || m || h || k) {
    for (int i = 0; i < MAX_N; i++)
      G[i].clear();
    for (int i = 0; i < m; i++) {
      int a, b, c, h, r;
      cin >> a >> b >> c >> h >> r;
      a--;
      b--;
      G[a].push_back(edge(b, c, h, r - 1));
      G[b].push_back(edge(a, c, h, r - 1));
    }
    int s, t, p;
    cin >> s >> t >> p;
    s--;
    t--;
    pass ps[p];
    for (int i = 0; i < p; i++) {
      int l;
      cin >> l >> ps[i].cost;
      for (int j = 0, x; j < l; j++)
        cin >> x, ps[i].bit |= (1 << (x - 1));
    }

    int pc[1 << k];
    fill(pc, pc + (1 << k), INF);
    pc[0] = 0;
    for (int i = 0; i < p; i++)
      for (int j = 0; j < (1 << k); j++)
        pc[j | ps[i].bit] = min(pc[j | ps[i].bit], pc[j] + ps[i].cost);

    int ans = INF;
    int dp[n][h + 1];
    for (int b = 0; b < (1 << k); b++) {
      if (pc[b] == INF)
        continue;
      for (int i = 0; i < n; i++)
        for (int j = 0; j <= h; j++)
          dp[i][j] = INF;
      priority_queue<PP, vector<PP>, greater<PP>> q;
      q.push(PP(0, P(0, s)));
      while (!q.empty()) {
        int d = q.top().F, t = q.top().S.F, v = q.top().S.S;
        q.pop();
        if (dp[v][t] <= d)
          continue;
        dp[v][t] = d;
        for (int i = 0; i < (int)G[v].size(); i++) {
          edge &e = G[v][i];
          if (t + e.hour > h)
            continue;
          if (((b >> e.com) & 1) && dp[e.to][t + e.hour] > d)
            q.push(PP(d, P(t + e.hour, e.to)));
          else if (dp[e.to][t + e.hour] > d + e.cost)
            q.push(PP(d + e.cost, P(t + e.hour, e.to)));
        }
      }
      int tmp = INF;
      for (int j = 0; j <= h; j++)
        tmp = min(tmp, dp[t][j]);
      ans = min(ans, tmp + pc[b]);
    }
    cout << (ans == INF ? -1 : ans) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX_N 111
#define INF 1 << 25
typedef pair<int, int> P;
typedef pair<int, P> PP;
#define F first
#define S second
struct edge {
  int to, cost, hour, com;
  edge() {}
  edge(int to, int cost, int hour, int com)
      : to(to), cost(cost), hour(hour), com(com) {}
};
struct pass {
  int bit, cost;
  pass() { bit = 0; }
  pass(int bit, int cost) : bit(bit), cost(cost) {}
};
vector<edge> G[MAX_N];
int main() {
  int n, m, h, k;
  while (cin >> n >> m >> h >> k, n || m || h || k) {
    for (int i = 0; i < MAX_N; i++)
      G[i].clear();
    for (int i = 0; i < m; i++) {
      int a, b, c, h, r;
      cin >> a >> b >> c >> h >> r;
      a--;
      b--;
      G[a].push_back(edge(b, c, h, r - 1));
      G[b].push_back(edge(a, c, h, r - 1));
    }
    int s, t, p;
    cin >> s >> t >> p;
    s--;
    t--;
    pass ps[p];
    for (int i = 0; i < p; i++) {
      int l;
      cin >> l >> ps[i].cost;
      for (int j = 0, x; j < l; j++)
        cin >> x, ps[i].bit |= (1 << (x - 1));
    }

    int pc[1 << k];
    fill(pc, pc + (1 << k), INF);
    pc[0] = 0;
    for (int i = 0; i < p; i++)
      for (int j = 0; j < (1 << k); j++)
        pc[j | ps[i].bit] = min(pc[j | ps[i].bit], pc[j] + ps[i].cost);

    int ans = INF;
    int dp[n][h + 1];
    for (int b = 0; b < (1 << k); b++) {
      if (pc[b] == INF)
        continue;
      for (int i = 0; i < n; i++)
        for (int j = 0; j <= h; j++)
          dp[i][j] = INF;
      priority_queue<PP, vector<PP>, greater<PP>> q;
      q.push(PP(0, P(0, s)));
      while (!q.empty()) {
        int d = q.top().F, t = q.top().S.F, v = q.top().S.S;
        q.pop();
        if (dp[v][t] <= d)
          continue;
        dp[v][t] = d;
        for (int i = 0; i < (int)G[v].size(); i++) {
          edge &e = G[v][i];
          if (t + e.hour > h)
            continue;
          if ((b >> e.com) & 1)
            q.push(PP(d, P(t + e.hour, e.to)));
          else
            q.push(PP(d + e.cost, P(t + e.hour, e.to)));
        }
      }
      int tmp = INF;
      for (int j = 0; j <= h; j++)
        tmp = min(tmp, dp[t][j]);
      ans = min(ans, tmp + pc[b]);
    }
    cout << (ans == INF ? -1 : ans) << endl;
  }
  return 0;
}