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

#include <algorithm>
#include <complex>
#include <iostream>
#include <vector>
#define X first
#define Y second
#define F 1145141919
using namespace std;

vector<pair<int, int>> v[160];
int dp[80][160][12], n, m, k;

int Jump(int a, int b, int c, int d) {
  if (v[a][b].X == -1 || v[c][d].X == -1)
    return 0;
  int ret = abs(v[a][b].X - v[c][d].X);
  ret *= v[a][b].Y + v[c][d].Y;
  return ret;
}

int main() {
  pair<int, int> p;
  p.X = -1, p.Y = -1;
  v[0].push_back(p);
  while (cin >> n >> m && n) {
    for (int i = 1; i <= n; i++) {
      cin >> k;
      v[i].clear();
      while (k--) {
        cin >> p.X >> p.Y;
        v[i].push_back(p);
      }
    }
    v[n + 1].clear();
    p.X = -1, p.Y = -1;
    v[n + 1].push_back(p);

    for (int i = 0; i < 80; i++)
      for (int j = 0; j < 160; j++)
        for (int k = 0; k < 12; k++)
          dp[i][j][k] = F;

    for (int i = 0; i <= m; i++)
      dp[i][0][0] = 0;

    for (int i = 0; i <= m; i++) {
      for (int f = 0; f <= n; f++) {
        for (int g = 0; g < v[f].size(); g++) {
          for (int k = 0; k < v[f + 1].size(); k++) {
            dp[i][f + 1][k] =
                min(dp[i][f + 1][k], dp[i][f][g] + Jump(f, g, f + 1, k));
          }
          if (f != n && i != m) {
            for (int k = 0; k < v[f + 2].size(); k++) {
              dp[i + 1][f + 2][k] =
                  min(dp[i + 1][f + 2][k], dp[i][f][g] + Jump(f, g, f + 2, k));
            }
          }
        }
      }
    }

    int ans = F;
    for (int i = 0; i <= m; i++) {
      ans = min(ans, dp[i][n + 1][0]);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <iostream>
using namespace std;
int dp[80][160][10];
int x[160][10], d[160][10], k[160];
int n, m;
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> k[i];
    for (int j = 0; j < k[i]; j++) {
      cin >> x[i][j] >> d[i][j];
    }
    if (!i)
      for (int mm = 0; mm <= m; mm++)
        for (int j = 0; j < k[i]; j++)
          dp[mm][i][j] = 0;
    else
      for (int mm = 0; mm <= m; mm++)
        for (int j = 0; j < k[i]; j++)
          dp[mm][i][j] = 1145141919;

    if (i) {
      for (int mm = 0; mm <= m; mm++) {
        for (int j = 0; j < k[i]; j++) {
          for (int r = 0; r < k[i - 1]; r++) {
            dp[mm][i][j] = min(dp[mm][i][j],
                               dp[mm][i - 1][r] + (abs(x[i][j] - x[i - 1][r]) *
                                                   (d[i][j] + d[i - 1][r])));
          }
        }
      }
    }
    if (i > 1) {
      for (int mm = 1; mm <= m; m++) {
        for (int j = 0; j < k[i]; j++) {
          for (int r = 0; r < k[i - 2]; r++) {
            dp[mm][i][j] = min(dp[mm][i][j], dp[mm - 1][i - 2][r] +
                                                 (abs(x[i][j] - x[i - 2][r]) *
                                                  (d[i][j] + d[i - 2][r])));
          }
        }
      }
    }
  }
  int ans = 1145141919;
  for (int mm = 0; mm <= m; mm++) {
    for (int j = 0; j < k[n - 1]; j++) {
      ans = min(ans, dp[mm][n - 1][j]);
    }
  }
  cout << ans << endl;
  return 0;
}