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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define MSG(a) cout << #a << " " << a << endl;
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}

const int INF = 1 << 30;
const int MAX_X = 1005;
ll dp[6][MAX_X][80];

int main() {
  int n, m;
  while (scanf("%d %d", &n, &m) != EOF && n) {
    rep(x, MAX_X) rep(y, 6) rep(d, m + 1) dp[y][x][d] = INF;
    vector<vint> r(MAX_X, vint(n + 10));
    vector<vint> xs(n);
    rep(i, MAX_X) rep(j, n) r[i][j] = 0;

    rep(i, n) {
      int s;
      scanf("%d", &s);
      rep(j, s) {
        int x, d;
        // ifs >> x >> d;
        scanf("%d %d", &x, &d);
        r[x][i] = d;
        xs[i].push_back(x);
      }
    }

    rep(x, MAX_X) if (r[x][0]) dp[0][x][m] = 0;
    rep(x, MAX_X) if (r[x][1]) dp[1][x][m - 1] = 0;

    for (int y = 1; y < n; y++) {
      if (1 < y)
        memcpy(dp[y % 5], dp[5], sizeof(dp[y % 5]));
      for (int x = 0; x < xs[y].size(); x++) {
        // ツ津環湘ュツジツδδδ督プ
        rep(nx, xs[y - 1].size()) rep(d, m + 1)
            // if(dp[(y-1)%5][xs[y-1][nx]][d] != INF)
            chmin(dp[y % 5][xs[y][x]][d],
                  dp[(y - 1) % 5][xs[y - 1][nx]][d] +
                      (r[xs[y][x]][y] + r[xs[y - 1][nx]][y - 1]) *
                          abs(xs[y][x] - xs[y - 1][nx]));

        // ツ暗ェツ段ツづづ篠つオ
        if (1 < y)
          rep(nx, xs[y - 2].size()) REP(d, 1, m + 1)
              // if(dp[(y-2)%5][xs[y-2][nx]][d] != INF)
              chmin(dp[y % 5][xs[y][x]][d - 1],
                    dp[(y - 2) % 5][xs[y - 2][nx]][d] +
                        (r[xs[y][x]][y] + r[xs[y - 2][nx]][y - 2]) *
                            abs(xs[y][x] - xs[y - 2][nx]));
      }
    }

    ll res = INF;
    rep(x, xs[n - 1].size()) rep(d, m + 1)
        chmin(res, dp[(n - 1) % 5][xs[n - 1][x]][d]);
    rep(x, xs[n - 2].size()) REP(d, 1, m + 1)
        chmin(res, dp[(n - 2) % 5][xs[n - 2][x]][d]);
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define MSG(a) cout << #a << " " << a << endl;
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}

const int INF = 1 << 30;
const int MAX_X = 1005;
ll dp[6][MAX_X][80];

int main() {
  // freopen("D:\\\\Users\\\\johnsmith\\\\documents\\\\visual studio
  // 2010\\\\Projects\\\\AOJ2\\\\AOJ2\\\\data\\\\2008-ho-t4-in10", "r", stdin); clock_t
  // start = clock(); ifstream ifs("D:\\\\Users\\\\johnsmith\\\\documents\\\\visual
  // studio 2010\\\\Projects\\\\AOJ2\\\\AOJ2\\\\data\\\\2008-ho-t4-in10");
  int n, m;
  while (scanf("%d %d", &n, &m) != EOF && n) {
    rep(x, MAX_X) rep(y, 6) rep(d, m + 1) dp[y][x][d] = INF;
    vector<vint> r(MAX_X, vint(n + 10));
    rep(i, MAX_X) rep(j, n) r[i][j] = 0;

    rep(i, n) {
      int s;
      scanf("%d", &s);
      rep(j, s) {
        int x, d;
        // ifs >> x >> d;
        scanf("%d %d", &x, &d);
        r[x][i] = d;
      }
    }

    rep(x, MAX_X) if (r[x][0]) dp[0][x][m] = 0;
    rep(x, MAX_X) if (r[x][1]) dp[1][x][m - 1] = 0;

    for (int y = 1; y < n; y++) {
      if (1 < y)
        memcpy(dp[y % 5], dp[5], sizeof(dp[y % 5]));
      for (int x = 0; x < MAX_X; x++) {

        if (r[x][y]) {
          // ツ津環湘ュツジツδδδ督プ
          rep(nx, MAX_X) rep(d, m + 1) if (dp[(y - 1) % 5][nx][d] != INF) chmin(
              dp[y % 5][x][d],
              dp[(y - 1) % 5][nx][d] + (r[x][y] + r[nx][y - 1]) * abs(x - nx));

          // ツ暗ェツ段ツづづ篠つオ
          if (1 < y)
            rep(nx, MAX_X) REP(d, 1, m + 1) if (dp[(y - 2) % 5][nx][d] != INF)
                chmin(dp[y % 5][x][d - 1],
                      dp[(y - 2) % 5][nx][d] +
                          (r[x][y] + r[nx][y - 2]) * abs(x - nx));
        }
      }
    }
    ll res = INF;
    rep(x, MAX_X) rep(d, m + 1) chmin(res, dp[(n - 1) % 5][x][d]);
    rep(x, MAX_X) REP(d, 1, m + 1) chmin(res, dp[(n - 2) % 5][x][d]);
    cout << res << endl;
  }
  // cout << clock() - start << endl;
}