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

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;

int n, m;
int d[76][151][1001];

int main() {
  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    memset(d, -1, sizeof(d));
    queue<pair<int, int>> qxy;
    queue<int> qm;
    queue<int> qs;
    vector<vector<pair<int, int>>> coor(n);
    int ans = INT_MAX / 2;
    REP(i, n) {
      int k;
      cin >> k;
      REP(j, k) {
        int a, b;
        cin >> a >> b;
        coor[i].PB(pair<int, int>(a, b));
        if (i == 0) {
          qxy.push(pair<int, int>(a, i));
          qm.push(m);
          qs.push(b);
          d[m][i][a] = 0;
        } else if (i == 1) {
          if (m == 0)
            continue;
          qxy.push(pair<int, int>(a, i));
          qm.push(m - 1);
          qs.push(b);
          d[m - 1][i][a] = 0;
        }
      }
    }

    while (!qxy.empty()) {
      int x = qxy.front().first;
      int y = qxy.front().second;
      int tm = qm.front();
      int ts = qs.front();
      qxy.pop();
      qm.pop();
      qs.pop();
      if (y + 1 >= n || (tm > 0 && y + 2 >= n)) {
        ans = min(ans, d[tm][y][x]);
        continue;
      }
      REP(i, coor[y + 1].size()) {
        int nx = coor[y + 1][i].first;
        int ny = y + 1;
        int slip = coor[y + 1][i].second;
        int v = d[tm][y][x] + (slip + ts) * abs(nx - x);
        if (d[tm][ny][nx] < 0 || d[tm][ny][nx] > v) {
          d[tm][ny][nx] = v;
          qxy.push(pair<int, int>(nx, ny));
          qm.push(tm);
          qs.push(slip);
        }
      }
      if (y + 2 >= n)
        continue;
      REP(i, coor[y + 2].size()) {
        int nx = coor[y + 2][i].first;
        int ny = y + 2;
        int slip = coor[y + 2][i].second;
        int v = d[tm][y][x] + (slip + ts) * abs(nx - x);
        if (tm > 0 && (d[tm - 1][ny][nx] < 0 || d[tm - 1][ny][nx] > v)) {
          d[tm - 1][ny][nx] = v;
          qxy.push(pair<int, int>(nx, ny));
          qm.push(tm - 1);
          qs.push(slip);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;

int n, m;
ll d[76][151][1001];

int main() {
  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    memset(d, -1, sizeof(d));
    queue<pair<int, int>> qxy;
    queue<int> qm;
    queue<int> qs;
    vector<vector<pair<int, int>>> coor(n);
    ll ans = 1000000000000000;
    REP(i, n) {
      int k;
      cin >> k;
      REP(j, k) {
        int a, b;
        cin >> a >> b;
        coor[i].PB(pair<int, int>(a, b));
        if (i == 0) {
          qxy.push(pair<int, int>(a, i));
          qm.push(m);
          qs.push(b);
          d[m][i][a] = 0;
        } else if (i == 1) {
          if (m == 0)
            continue;
          qxy.push(pair<int, int>(a, i));
          qm.push(m - 1);
          qs.push(b);
          d[m - 1][i][a] = 0;
        }
      }
    }

    while (!qxy.empty()) {
      int x = qxy.front().first;
      int y = qxy.front().second;
      int tm = qm.front();
      int ts = qs.front();
      qxy.pop();
      qm.pop();
      qs.pop();
      if (y + 1 >= n || (tm > 0 && y + 2 >= n)) {
        ans = min(ans, d[tm][y][x]);
        continue;
      }
      REP(i, coor[y + 1].size()) {
        int nx = coor[y + 1][i].first;
        int ny = y + 1;
        int slip = coor[y + 1][i].second;
        int v = d[tm][y][x] + (slip + ts) * abs(nx - x);
        if (d[tm][ny][nx] < 0 || d[tm][ny][nx] > v) {
          d[tm][ny][nx] = v;
          qxy.push(pair<int, int>(nx, ny));
          qm.push(tm);
          qs.push(slip);
        }
      }
      if (y + 2 >= n)
        continue;
      REP(i, coor[y + 2].size()) {
        int nx = coor[y + 2][i].first;
        int ny = y + 2;
        int slip = coor[y + 2][i].second;
        int v = d[tm][y][x] + (slip + ts) * abs(nx - x);
        if (tm > 0 && (d[tm - 1][ny][nx] < 0 || d[tm - 1][ny][nx] > v)) {
          d[tm - 1][ny][nx] = v;
          qxy.push(pair<int, int>(nx, ny));
          qm.push(tm - 1);
          qs.push(slip);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}