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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;

const ll INF = 9000000000000000000;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

int N, M;
std::vector<P> S[153];
ll dp[153][20][80];

ll value(const P &lp, const P &rp) {
  return (lp.second + rp.second) * std::abs(lp.first - rp.first);
}

ll rec(int y, int x, int t) {
  if (y > N) {
    return 0;
  }
  if (dp[y][x][t] != -1) {
    return dp[y][x][t];
  }

  ll res = INF;
  REP(i, S[y + 1].size()) {
    res = std::min(res, rec(y + 1, i, t) + value(S[y][x], S[y + 1][i]));
  }

  if (t < M) {
    REP(i, S[y + 2].size()) {
      res = std::min(res, rec(y + 2, i, t + 1) + value(S[y][x], S[y + 2][i]));
    }
  }

  return dp[y][x][t] = res;
}

int main() {
  while (std::cin >> N >> M, N) {
    REP(i, N + 3) { S[i].clear(); }

    REP(i, N) {
      int k;
      std::cin >> k;

      REP(j, k) {
        int x, d;
        std::cin >> x >> d;
        x--;

        S[i + 1].push_back(mp(x, d));
      }
    }

    REP(i, S[1].size()) { S[0].push_back(S[1][i]); }

    REP(i, S[2].size()) { S[0].push_back(S[2][i]); }

    REP(i, S[N].size()) {
      S[N + 1].push_back(S[N][i]);
      S[N + 2].push_back(S[N][i]);
    }

    REP(i, S[N - 1].size()) { S[N + 1].push_back(S[N - 1][i]); }

    REP(i, N + 3) {
      REP(j, 20) {
        REP(k, M + 1) { dp[i][j][k] = -1; }
      }
    }

    ll res = INF;
    REP(i, S[0].size()) { res = std::min(res, rec(0, i, 0)); }

    std::cout << res << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair

typedef unsigned long long ull;
typedef std::pair<int, int> P;

const ull INF = 1 << 31;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

int N, M;
std::vector<P> S[153];
std::map<int, int> dp[153][80]; // [153][80][1000];

ull value(const P &lp, const P &rp) {
  return (lp.second + rp.second) * std::abs(lp.first - rp.first);
}

ull rec(int y, int x, int t) {
  if (y > N) {
    return 0;
  }
  if (dp[y][t][x] != 0) {
    return dp[y][t][x];
  }

  ull res = INF;
  REP(i, S[y + 1].size()) {
    res = std::min(res, rec(y + 1, i, t) + value(S[y][x], S[y + 1][i]));
  }

  if (t < M) {
    REP(i, S[y + 2].size()) {
      res = std::min(res, rec(y + 2, i, t + 1) + value(S[y][x], S[y + 2][i]));
    }
  }

  return dp[y][t][x] = res;
}

int main() {
  while (std::cin >> N >> M, N) {
    REP(i, N + 3) {
      S[i].clear();

      REP(j, M + 1) { dp[i][j].clear(); }
    }

    REP(i, N) {
      int k;
      std::cin >> k;

      REP(j, k) {
        int x, d;
        std::cin >> x >> d;
        x--;

        S[i + 1].push_back(mp(x, d));
      }
    }

    REP(i, 1000) {
      S[0].push_back(mp(i, 0));
      S[N + 1].push_back(mp(i, 0));
      S[N + 2].push_back(mp(i, 0));
    }

    ull res = INF;
    REP(i, 1000) { res = std::min(res, rec(0, i, 0)); }

    std::cout << res << std::endl;
  }
}