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

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

// dp[i][j][k]->iñÌòÎµWvðcµ,jsÚ,kñÚÉéÅ¬RXg
int dp[3][1001][77];
int field[151][1001];
const int INF = 1000000000;

int main() {

  int n, m;
  while (cin >> n >> m && !(n == 0 && m == 0)) {
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 1001; j++)
        fill(dp[i][j], dp[i][j] + 77, INF);

    int(*prv)[77] = dp[2];
    int(*pprv)[77] = dp[1];
    int(*cur)[77] = dp[0];
    memset(field, 0, sizeof(field));
    for (int i = 0; i < n; i++) {
      int k;
      cin >> k;
      for (int j = 0; j < k; j++) {
        int x, d;
        cin >> x >> d;
        field[i][x - 1] = d;
      }
    }
    // ún_ÌÝè
    for (int i = 0; i < 1001; i++) {
      if (field[0][i] != 0)
        dp[0][i][m] = 0;
    }
    if (m != 0) {
      for (int i = 0; i < 1001; i++) {
        if (field[1][i] != 0)
          dp[1][i][m - 1] = 0;
      }
    }

    // dpÅÅ¬RXgðßé
    for (int i = 1; i < n; i++) {
      swap(cur, prv);
      swap(pprv, cur);
      if (m != 0 && i == 1) {

      } else {
        for (int j = 0; j < 1001; j++)
          fill(cur[j], cur[j] + 77, INF);
      }
      // Ç±ÖWv·é©
      for (int j = 0; j < 1001; j++) {
        if (field[i][j] == 0)
          continue;
        // Ç±©çWv·é©
        for (int k = 0; k < 1001; k++) {
          if (field[i - 1][k] == 0 && (i <= 1 || field[i - 2][k] == 0))
            continue;
          // ÇÌRXgÅWv·é©
          for (int l = m; l >= 0; l--) {
            // ÓÂ¤ÉWv
            int cost = ((field[i - 1][k] + field[i][j]) * abs(k - j));
            cur[j][l] = min(cur[j][l], prv[k][l] + cost);
            if (i >= 2 && l != m) {
              // êÂòÎµÅWv
              cost = ((field[i - 2][k] + field[i][j]) * abs(k - j));
              cur[j][l] = min(cur[j][l], pprv[k][l + 1] + cost);
            }
          }
        }
      }
    }
    int minRes = INF;
    for (int i = 0; i < 1001; i++) {
      for (int j = 0; j <= m; j++)
        minRes = min(minRes, cur[i][j]);
      for (int j = 1; j <= m; j++)
        minRes = min(minRes, prv[i][j]);
    }
    cout << minRes << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

// dp[i][j][k]->iñÌòÎµWvðcµ,jsÚ,kñÚÉéÅ¬RXg
int dp[3][1001][77];
int field[151][1001];
const int INF = 100000000;

int main() {

  int n, m;
  while (cin >> n >> m && !(n == 0 && m == 0)) {
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 1001; j++)
        fill(dp[i][j], dp[i][j] + 77, INF);

    int(*prv)[77] = dp[2];
    int(*pprv)[77] = dp[1];
    int(*cur)[77] = dp[0];
    memset(field, 0, sizeof(field));
    for (int i = 0; i < n; i++) {
      int k;
      cin >> k;
      for (int j = 0; j < k; j++) {
        int x, d;
        cin >> x >> d;
        field[i][x - 1] = d;
      }
    }
    // ún_ÌÝè
    for (int i = 0; i < 1001; i++) {
      if (field[0][i] != 0) {
        dp[0][i][m] = 0;
      }
    }
    int(*fin)[77];
    // dpÅÅ¬RXgðßé
    for (int i = 1; i < n; i++) {
      swap(cur, prv);
      swap(pprv, cur);
      for (int i = 0; i < 1001; i++)
        fill(cur[i], cur[i] + 77, INF);
      // Ç±ÖWv·é©
      for (int j = 0; j < 1001; j++) {
        if (field[i][j] == 0)
          continue;
        // Ç±©çWv·é©
        for (int k = 0; k < 1001; k++) {
          // ÇÌRXgÅWv·é©
          for (int l = m; l >= max(0, m - i); l--) {
            // ÓÂ¤ÉWv
            int cost = ((field[i - 1][k] + field[i][j]) * abs(k - j));
            cur[j][l] = min(cur[j][l], prv[k][l] + cost);
            if (i >= 2 && l != m) {
              // êÂòÎµÅWv
              cost = ((field[i - 2][k] + field[i][j]) * abs(k - j));
              cur[j][l] = min(cur[j][l], pprv[k][l + 1] + cost);
            }
          }
        }
      }
      fin = cur;
    }
    int minRes = INF;
    for (int i = 0; i < 1001; i++) {
      for (int j = 0; j <= m; j++) {
        minRes = min(minRes, fin[i][j]);
      }
    }
    cout << minRes << endl;
  }

  return 0;
}