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

#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

inline bool cheak(int x, int y, int xMax, int yMax) {
  return x >= 0 && y >= 0 && xMax > x && yMax > y;
}
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef long long ll;
typedef unsigned long long ull;

#define For(i, a, b) for (ll(i) = (a); i < (b); (i)++)
#define rep(i, n) For(i, 0, n)
#define clr(a) memset((a), 0, sizeof(a))
#define mclr(a) memset((a), -1, sizeof(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (sizeof(a))
#define Fill(a, v) fill((int *)a, (int *)(a + (sz(a) / sz(*(a)))), v)

const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1},
          dy[8] = {0, -1, 0, 1, -1, 1, -1, 1};
const int mod = 1000000007;
const int INF = 1e9;

int main() {

  int n, m, H, K;
  while (cin >> n >> m >> H >> K && n) {

    int dc[101][101];
    int dh[101][101];
    int dr[101][101];

    rep(i, 101) rep(j, 101) dc[i][j] = -1;
    rep(i, m) {
      int a, b, c, h, r;
      cin >> a >> b >> c >> h >> r;
      dc[a][b] = dc[b][a] = c;
      dh[a][b] = dh[b][a] = h;
      dr[a][b] = dr[b][a] = r - 1;
    }
    int s, t;
    cin >> s >> t;
    int p;
    cin >> p;

    int pd[1000];
    int pt[1000];
    rep(i, p) {
      int l;
      cin >> l >> pd[i];
      pt[i] = 0;
      rep(j, l) {
        int dt;
        cin >> dt;
        dt--;
        pt[i] |= (1 << dt);
      }
    }

    int ans = INF;

    int dval[1024];
    rep(i, 1024) dval[i] = INF;
    dval[0] = 0;
    for (int i = 0; i <= (1 << K); i++)
      if (dval[i] != INF) {
        for (int k = 0; k < p; k++) {
          dval[i | pt[k]] = min(dval[i | pt[k]], dval[i] + pd[k]);
        }
      }
    int d[101][25];

    for (int f = 0; f <= (1 << K); f++) {

      int val = dval[f];
      if (val != INF) {

        rep(i, 101) rep(k, 25) d[i][k] = INF;
        d[s][0] = 0;

        queue<T> q;
        q.push(T(s, 0, 0));
        while (q.size()) {
          int nw = get<0>(q.front()), c = get<1>(q.front()),
              h = get<2>(q.front());
          q.pop();

          For(i, 1, n + 1) {
            if (dc[nw][i] != -1) {

              int tc, th = h + dh[nw][i];
              if (!(f >> dr[nw][i] & 1))
                tc = c + dc[nw][i];
              else
                tc = c;

              if (th <= H && val + tc < ans) {
                if (d[i][th] > tc) {
                  d[i][th] = tc;
                  if (i != t) {
                    q.push(T(i, tc, th));
                  } else {
                    ans = min(ans, val + tc);
                  }
                }
              }
            }
          }
        }
      }
    }

    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

inline bool cheak(int x, int y, int xMax, int yMax) {
  return x >= 0 && y >= 0 && xMax > x && yMax > y;
}
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef long long ll;
typedef unsigned long long ull;

#define For(i, a, b) for (ll(i) = (a); i < (b); (i)++)
#define rep(i, n) For(i, 0, n)
#define clr(a) memset((a), 0, sizeof(a))
#define mclr(a) memset((a), -1, sizeof(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (sizeof(a))
#define Fill(a, v) fill((int *)a, (int *)(a + (sz(a) / sz(*(a)))), v)

const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1},
          dy[8] = {0, -1, 0, 1, -1, 1, -1, 1};
const int mod = 1000000007;
const int INF = 1e9;

int main() {

  int n, m, H, K;
  while (cin >> n >> m >> H >> K && n) {

    int dc[101][101];
    int dh[101][101];
    int dr[101][101];

    rep(i, 101) rep(j, 101) dc[i][j] = -1;
    rep(i, m) {
      int a, b, c, h, r;
      cin >> a >> b >> c >> h >> r;
      dc[a][b] = dc[b][a] = c;
      dh[a][b] = dh[b][a] = h;
      dr[a][b] = dr[b][a] = r - 1;
    }
    int s, t;
    cin >> s >> t;
    int p;
    cin >> p;

    int pd[1000];
    int pt[1000];
    rep(i, p) {
      int l;
      cin >> l >> pd[i];
      pt[i] = 0;
      rep(j, l) {
        int dt;
        cin >> dt;
        dt--;
        pt[i] |= (1 << dt);
      }
    }

    int ans = INF;

    int dval[1024];
    rep(i, 1024) dval[i] = INF;
    dval[0] = 0;
    for (int i = 0; i <= (1 << K); i++)
      if (dval[i] != INF) {
        for (int k = 0; k < p; k++) {
          dval[i | pt[k]] = min(dval[i | pt[k]], dval[i] + pd[k]);
        }
      }
    int d[101][2];

    for (int f = 0; f <= (1 << K); f++) {

      int val = dval[f];
      if (val != INF) {

        rep(i, 101) rep(k, 2) d[i][k] = INF;
        d[s][0] = d[s][1] = 0;

        queue<T> q;
        q.push(T(s, 0, 0));
        while (q.size()) {
          int nw = get<0>(q.front()), c = get<1>(q.front()),
              h = get<2>(q.front());
          q.pop();

          For(i, 1, n + 1) {
            if (dc[nw][i] != -1) {

              int tc;
              if (!(f >> dr[nw][i] & 1))
                tc = c + dc[nw][i];
              else
                tc = c;

              if (h + dh[nw][i] <= H &&
                  (tc <= d[i][0] || h + dh[nw][i] <= d[i][1])) {

                if (val + tc < ans) {

                  d[i][0] = min(d[i][0], tc);
                  d[i][1] = min(d[i][1], h + dh[nw][i]);
                  q.push(T(i, tc, h + dh[nw][i]));
                }
              }
            }
          }
        }
        ans = min(ans, val + d[t][0]);
      }
    }

    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }

  return 0;
}