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

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, 0, -1, 1, -1, -1, 1};
const int dy[] = {0, 1, -1, 0, 1, 1, -1, -1};
const int INF = 1 << 30;
const double EPS = 1e-8;
#define PB push_back
#define mk make_pair
#define fr first
#define sc second
#define reps(i, j, k) for (int i = (j); i < (k); ++i)
#define rep(i, j) reps(i, 0, j)
#define MOD 1000000007
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
typedef pair<int, int> Pii;
typedef pair<int, Pii> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;
struct data {
  int to;
  int fare;
  int hour;
  int admin;
  data() {}
  data(int _to, int _fare, int _hour, int _admin) {
    to = _to;
    fare = _fare;
    hour = _hour;
    admin = _admin;
  }
  bool operator<(const data &a) const {
    if (fare != a.fare)
      return fare > a.fare;
    return hour > a.hour;
  }
};
vector<data> Graph[128];
int ps[258];
int dp[258];
int start, goal;
int H;
void init() {
  rep(i, 128) { Graph[i].clear(); }
  rep(i, (1 << 8) + 1) { dp[i] = INF; }
  return;
}

int dijkstra(int pass, int ma) {
  int memo[25][101];
  rep(i, 25) {
    rep(j, 101) { memo[i][j] = INF; }
  }
  int ans = INF;
  priority_queue<data> Q;
  Q.push(data(start, dp[pass], 0, pass));
  while (!Q.empty()) {
    data e = Q.top();
    Q.pop();
    if (e.hour > H)
      continue;
    if (memo[e.hour][e.to] != INF)
      continue;
    memo[e.hour][e.to] = e.fare;
    if (e.to == goal) {
      ans = e.fare;
      break;
    }
    if (e.fare > ma) {
      break;
    }
    rep(i, Graph[e.to].size()) {
      if (e.admin & (1 << Graph[e.to][i].admin)) {
        Q.push(data(Graph[e.to][i].to, e.fare, e.hour + Graph[e.to][i].hour,
                    e.admin));
      } else {
        Q.push(data(Graph[e.to][i].to, e.fare + Graph[e.to][i].fare,
                    e.hour + Graph[e.to][i].hour, e.admin));
      }
    }
  }
  return ans;
}

int main() {
  int N, M, K;
  while (true) {
    scanf("%d%d%d%d", &N, &M, &H, &K);
    if (N == M && M == H && H == K && K == 0)
      break;
    init();
    rep(i, M) {
      int a, b, c, h, r;
      scanf("%d%d%d%d%d", &a, &b, &c, &h, &r);
      --a;
      --b;
      --r;
      Graph[a].PB(data(b, c, h, r));
      Graph[b].PB(data(a, c, h, r));
    }
    scanf("%d%d", &start, &goal);
    --start;
    --goal;
    int P;
    scanf("%d", &P);
    rep(i, P) {
      int m, c;
      scanf("%d%d", &m, &c);
      int bit = 0;
      rep(j, m) {
        int tmp;
        scanf("%d", &tmp);
        bit += 1 << (tmp - 1);
      }
      ps[i] = bit;
      dp[bit] = c;
    }
    int ans = INF;
    dp[0] = 0;
    rep(i, 1 << K) {
      if (dp[i] > ans)
        continue;
      ans = min(ans, dijkstra(i, ans));
      rep(j, P) { dp[i | ps[j]] = min(dp[i | ps[j]], dp[i] + dp[ps[j]]); }
    }
    if (ans == INF) {
      ans = -1;
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, 0, -1, 1, -1, -1, 1, 2, 0, -2, 0};
const int dy[] = {0, 1, -1, 0, 1, 1, -1, -1, 0, 2, 0, -2};
const int INF = 1 << 30;
const double EPS = 1e-8;
#define PB push_back
#define mk make_pair
#define fr first
#define sc second
#define reps(i, j, k) for (int i = (j); i < (k); ++i)
#define rep(i, j) reps(i, 0, j)
#define MOD 1000000007
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
typedef pair<int, int> Pii;
typedef pair<int, Pii> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;
struct data {
  int to;
  int fare;
  int hour;
  int admin;
  data() {}
  data(int _to, int _fare, int _hour, int _admin) {
    to = _to;
    fare = _fare;
    hour = _hour;
    admin = _admin;
  }
  bool operator<(const data &a) const {
    /*if(hour == a.hour)return fare>a.fare;
    return hour>a.hour;
    */
    if (fare == a.fare)
      return hour > a.hour;
    return fare > a.fare;
  }
};
vector<data> Graph[128];
int memo[101][1 << 9];
void initMemo() {
  rep(i, 101) {
    rep(j, 1 << 9) { memo[i][j] = INF; }
  }
  return;
}

int main() {
  int N, M, H, K;
  while (scanf("%d%d%d%d", &N, &M, &H, &K), (N | M | H | K)) {
    rep(i, 128) { Graph[i].clear(); }
    rep(i, M) {
      int a, b, c, h, r;
      scanf("%d%d%d%d%d", &a, &b, &c, &h, &r);
      Graph[a].PB(data(b, c, h, r));
      Graph[b].PB(data(a, c, h, r));
    }
    int start, goal;
    scanf("%d%d", &start, &goal);
    int P;
    vector<Pii> passport;
    scanf("%d", &P);
    rep(i, P) {
      int m, c;
      scanf("%d%d", &m, &c);
      int bit = 0;
      rep(j, m) {
        int tmp;
        scanf("%d", &tmp);
        bit += 1 << tmp;
      }
      passport.PB(Pii(c, bit));
    }
    priority_queue<data> Q;
    Q.push(data(start, 0, 0, 0));
    initMemo();
    while (!Q.empty()) {
      data e = Q.top();
      Q.pop();
      if (e.hour > H)
        continue;
      if (memo[e.to][e.admin] != INF)
        continue;
      memo[e.to][e.admin] = e.fare;

      rep(i, Graph[e.to].size()) {
        if (1 << Graph[e.to][i].admin & e.admin) {
          Q.push(data(Graph[e.to][i].to, e.fare, e.hour + Graph[e.to][i].hour,
                      e.admin));
        } else {
          rep(j, passport.size()) {
            if (1 << Graph[e.to][i].admin & passport[j].sc) {
              Q.push(data(Graph[e.to][i].to, e.fare + passport[j].fr,
                          e.hour + Graph[e.to][i].hour,
                          e.admin | passport[j].sc));
            }
          }
          Q.push(data(Graph[e.to][i].to, e.fare + Graph[e.to][i].fare,
                      e.hour + Graph[e.to][i].hour, e.admin));
        }
      }
    }
    int ans = INF;
    rep(i, 1 << 9) { ans = min(ans, memo[goal][i]); }
    if (ans == INF) {
      ans = -1;
    }
    printf("%d\\n", ans);
  }
  return 0;
}