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

#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define EPS 1e-9

map<int, int> dp[80][160]; // [m][i]<x,cost>

int main() {
  int n, m;

  while (scanf("%d%d", &n, &m), n | m) {
    int ans = INT_MAX;
    vector<pair<int, int>> vp[160];
    rep(i, 80) rep(j, 160) dp[i][j].clear();

    rep(i, n) {
      vector<pair<int, int>> tmp;
      int sz;
      scanf("%d", &sz);
      rep(j, sz) {
        int a, b;
        scanf("%d%d", &a, &b);
        // printf("a,b , %d,%d\\n",a,b);
        tmp.push_back(make_pair(a, b));
      }
      vp[i] = tmp;
    }

    rep(i, n) {
      if (i == 0) {
        rep(j, vp[0].size()) { dp[0][0][vp[0][j].first] = 0; }
        if (n > 1 && m > 0) {
          rep(j, vp[1].size()) { dp[1][1][vp[1][j].first] = 0; }
        }
      } else {
        rep(l, m + 1) {
          rep(j, vp[i - 1].size()) if (dp[l][i - 1].count(
                                           vp[i - 1][j].first)) { // src 1step
            rep(k, vp[i].size()) {                                // dest
              int c = (vp[i - 1][j].second + vp[i][k].second) *
                      abs(vp[i - 1][j].first - vp[i][k].first);
              if (dp[l][i].count(vp[i][k].first)) {
                dp[l][i][vp[i][k].first] =
                    min(dp[l][i][vp[i][k].first],
                        dp[l][i - 1][vp[i - 1][j].first] + c);
              } else {
                dp[l][i][vp[i][k].first] = dp[l][i - 1][vp[i - 1][j].first] + c;
              }
            }
          }
          if (i > 1 && l < m) {
            rep(j, vp[i - 2].size()) if (dp[l][i - 2].count(
                                             vp[i - 2][j].first)) { // src 2step
              rep(k, vp[i].size()) {                                // dest
                int c = (vp[i - 2][j].second + vp[i][k].second) *
                        abs(vp[i - 2][j].first - vp[i][k].first);
                if (dp[l + 1][i].count(vp[i][k].first)) {
                  dp[l + 1][i][vp[i][k].first] =
                      min(dp[l + 1][i][vp[i][k].first],
                          dp[l][i - 2][vp[i - 2][j].first] + c);
                } else {
                  dp[l + 1][i][vp[i][k].first] =
                      dp[l][i - 2][vp[i - 2][j].first] + c;
                }
              }
            }
          }
        }
      }
    }

    /*
    rep(i,n){
            rep(j,10){
                    rep(k,m+1){
                            printf("dp[%d][%d][%d] : %d\\n",k,i,j,dp[k][i][j]);
                    }
            }
    }
    */

    rep(l, m + 1) {
      rep(i, vp[n - 1].size()) if (dp[l][n - 1].count(vp[n - 1][i].first)) {
        ans = min(ans, dp[l][n - 1][vp[n - 1][i].first]);
      }
      if (n - 2 >= 0 && l < m) {
        rep(i, vp[n - 2].size()) if (dp[l][n - 2].count(vp[n - 2][i].first)) {
          ans = min(ans, dp[l][n - 2][vp[n - 2][i].first]);
        }
      }
    }
    // rep(i,m+1)rep(j,st_sz[n])
    //	ans = min(ans,dp[i][n][j]);

    printf("%d\\n", ans);
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define EPS 1e-9

int dp[80][160][1024];

int main() {
  int n, m;

  while (scanf("%d%d", &n, &m), n | m) {
    int ans = INT_MAX;
    vector<pair<int, int>> vp[160];
    rep(i, 80) rep(j, 160) rep(k, 1024) dp[i][j][k] = INT_MAX;

    rep(i, n) {
      vector<pair<int, int>> tmp;
      int sz;
      scanf("%d", &sz);
      rep(j, sz) {
        int a, b;
        scanf("%d%d", &a, &b);
        // printf("a,b , %d,%d\\n",a,b);
        tmp.push_back(make_pair(a, b));
      }
      vp[i] = tmp;
    }

    rep(i, n) {
      if (i == 0) {
        rep(j, vp[0].size()) { dp[0][0][vp[0][j].first] = 0; }
        if (n > 1 && m > 0) {
          rep(j, vp[1].size()) { dp[1][1][vp[1][j].first] = 0; }
        }
      } else {
        rep(l, m + 1) {
          rep(j, vp[i - 1].size()) if (dp[l][i - 1][vp[i - 1][j].first] !=
                                       INT_MAX) { // src 1step
            rep(k, vp[i].size()) {                // dest
              int c = (vp[i - 1][j].second + vp[i][k].second) *
                      abs(vp[i - 1][j].first - vp[i][k].first);
              dp[l][i][vp[i][k].first] =
                  min(dp[l][i][vp[i][k].first],
                      dp[l][i - 1][vp[i - 1][j].first] + c);
            }
          }
          if (i > 1 && l < m) {
            rep(j, vp[i - 2].size()) if (dp[l][i - 2][vp[i - 2][j].first] !=
                                         INT_MAX) { // src 2step
              rep(k, vp[i].size()) {                // dest
                int c = (vp[i - 2][j].second + vp[i][k].second) *
                        abs(vp[i - 2][j].first - vp[i][k].first);
                dp[l + 1][i][vp[i][k].first] =
                    min(dp[l + 1][i][vp[i][k].first],
                        dp[l][i - 2][vp[i - 2][j].first] + c);
              }
            }
          }
        }
      }
    }

    /*
    rep(i,n){
            rep(j,10){
                    rep(k,m+1){
                            printf("dp[%d][%d][%d] : %d\\n",k,i,j,dp[k][i][j]);
                    }
            }
    }
    */

    rep(l, m + 1) {
      rep(i, vp[n - 1].size()) {
        ans = min(ans, dp[l][n - 1][vp[n - 1][i].first]);
      }
      if (n - 2 >= 0 && l < m) {
        rep(i, vp[n - 2].size()) {
          ans = min(ans, dp[l][n - 2][vp[n - 2][i].first]);
        }
      }
    }
    // rep(i,m+1)rep(j,st_sz[n])
    //	ans = min(ans,dp[i][n][j]);

    printf("%d\\n", ans);
  }

  return 0;
}