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

#include <bits/stdc++.h>
using namespace std;
const long long INF = 50000000000;

int n, m;
int K[152], x[152][10], d[152][10];
long long dp[152][10];

void init() {
  dp[0][0] = 0;
  for (int i = 1; i <= 151; i++) {
    for (int j = 0; j < 10; j++) {
      dp[i][j] = INF;
    }
  }
  memset(K, 0, sizeof(K));
  K[0] = 1;
  K[n + 1] = 1;
  memset(x, 0, sizeof(x));
  memset(d, 0, sizeof(d));
}

int main() {
  while (scanf("%d %d", &n, &m), n + m) {
    init();

    for (int i = 1; i <= n; i++) {
      scanf("%d", &K[i]);

      for (int j = 0; j < K[i]; j++) {
        scanf("%d %d", &x[i][j], &d[i][j]);
      }
    }

    for (int i = 0; i <= n; i++) {
      for (int j = 0; j < K[i]; j++) {
        for (int k = 0; k < K[i + 1]; k++) {
          if (i == 0 || i == n)
            dp[i + 1][k] = min(dp[i + 1][k], dp[i][j]);
          else
            dp[i + 1][k] =
                min(dp[i + 1][k], dp[i][j] + (d[i][j] + d[i + 1][k]) *
                                                 abs(x[i][j] - x[i + 1][k]));
        }
      }
    }

    while (m--) {
      for (int i = n - 1; i >= 0; i--) {
        for (int j = 0; j < K[i]; j++) {
          for (int k = 0; k < K[i + 2]; k++) {
            if (i == 0 || i == n - 1)
              dp[i + 2][k] = min(dp[i + 2][k], dp[i][j]);
            else
              dp[i + 2][k] =
                  min(dp[i + 2][k], dp[i][j] + (d[i][j] + d[i + 2][k]) *
                                                   abs(x[i][j] - x[i + 2][k]));
          }
        }
      }

      for (int i = 0; i <= n; i++) {
        for (int j = 0; j < K[i]; j++) {
          for (int k = 0; k < K[i + 1]; k++) {
            if (i == 0 || i == n)
              dp[i + 1][k] = min(dp[i + 1][k], dp[i][j]);
            else
              dp[i + 1][k] =
                  min(dp[i + 1][k], dp[i][j] + (d[i][j] + d[i + 1][k]) *
                                                   abs(x[i][j] - x[i + 1][k]));
          }
        }
      }
    }

    printf("%lld\\n", dp[n + 1][0]);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF (1000000000)
#define MOD (1000000007)
using namespace std;

typedef vector<int> VI;
typedef vector<int>::iterator VII;
typedef vector<double> VD;
typedef vector<double>::iterator VDI;
typedef vector<long>::iterator VL;
typedef vector<long>::iterator VLI;
typedef pair<int, int> P;

int d[150][1000];
int dp[150][1000][100];

int main() {
  int n, m;
  while (scanf("%d %d", &n, &m), n + m) {
    memset(d, 0, sizeof(d));
    int maxx = 0;

    for (int i = 0; i < n; i++) {
      int k;
      scanf("%d", &k);
      for (int j = 0; j < k; j++) {
        int x;
        scanf("%d", &x);
        x--;
        scanf("%d", &d[i][x]);

        maxx = max(maxx, x);
      }
    }

    for (int i = 0; i < 150; i++) {
      for (int j = 0; j <= maxx; j++) {
        for (int k = 0; k < 100; k++) {
          if (i == 0 && d[i][j] && k == 0)
            dp[i][j][k] = 0;
          else
            dp[i][j][k] = INF;
        }
      }
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= maxx; j++) {
        for (int k = 0; k <= maxx; k++) {
          for (int l = 0; l <= m; l++) {
            if (d[i][j] && d[i + 1][k]) {
              dp[i + 1][k][l] =
                  min(dp[i + 1][k][l],
                      dp[i][j][l] + (d[i][j] + d[i + 1][k]) * abs(j - k));
              // printf("%d\\n", dp[i + 1][k][l]);
            }

            if (i < n - 1 && d[i][j] && d[i + 2][k]) {
              dp[i + 2][k][l + 1] =
                  min(dp[i + 2][k][l + 1],
                      dp[i][j][l] + (d[i][j] + d[i + 2][k]) * abs(j - k));
            }
          }
        }
      }
    }

    int mini = INF;
    for (int i = 0; i <= maxx; i++) {
      for (int j = 0; j <= m; j++) {
        mini = min(mini, dp[n - 1][i][j]);
      }
    }

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

  return 0;
}