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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;

void calc(int n, int m) {
  int c, a, b, d = -1, dp[151][10][80], ans = 1000000000, s, dis;
  vector<pair<int, int>> stone[151];

  // scanf("%d%d",&n,&m);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 10; j++) {
      for (int k = 0; k <= m; k++) {
        dp[i][j][k] = 1000000000;
      }
    }
  }

  for (int j = 0; j < 10; j++) {
    dp[0][j][0] = 0;
    dp[1][j][1] = 0;
  }

  for (int i = 0; i < n; i++) {
    scanf("%d", &c);

    for (int j = 0; j < c; j++) {
      scanf("%d%d", &a, &b);
      stone[i].push_back(make_pair(a, b));
    }
  }

  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < stone[i].size(); j++) {

      for (int k = 0; k < stone[i + 1].size(); k++) {
        for (int l = 0; l <= m; l++) {
          s = stone[i][j].second + stone[i + 1][k].second;
          dis = abs(stone[i][j].first - stone[i + 1][k].first);
          dp[i + 1][k][l] = min(dp[i + 1][k][l], dp[i][j][l] + s * dis);
        }
      }
      for (int k = 0; k < stone[i + 2].size(); k++) {
        for (int l = 0; l < m; l++) {
          s = stone[i][j].second + stone[i + 2][k].second;
          dis = abs(stone[i][j].first - stone[i + 2][k].first);
          dp[i + 2][k][l + 1] = min(dp[i + 2][k][l + 1], dp[i][j][l] + s * dis);
        }
      }
    }
  }

  for (int i = 0; i < stone[n - 1].size(); i++) {
    for (int j = 0; j <= m; j++) {
      ans = min(ans, dp[n - 1][i][j]);
    }
  }

  for (int i = 0; i < stone[n - 2].size(); i++) {
    for (int j = 0; j < m; j++) {
      ans = min(ans, dp[n - 2][i][j]);
    }
  }

  printf("%d\\n", ans);

  return;
}

int main() {
  int n, m;

  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0)
      break;
    calc(n, m);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;

void calc(int n, int m) {
  int c, a, b, dp[151][10][80], ans = 1000000000;
  vector<pair<int, int>> stone[150];

  // scanf("%d%d",&n,&m);

  for (int j = 0; j < 10; j++) {
    for (int k = 0; k <= m; k++) {
      dp[0][j][k] = 0;
    }
  }

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 10; j++) {
      for (int k = 0; k <= m; k++) {
        dp[i][j][k] = 1000000000;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    scanf("%d", &c);

    for (int j = 0; j < c; j++) {
      scanf("%d%d", &a, &b);
      stone[i].push_back(make_pair(a, b));
    }
  }

  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < stone[i].size(); j++) {
      for (int k = 0; k < stone[i + 1].size(); k++) {
        for (int l = 0; l <= m; l++) {
          dp[i + 1][k][l] = min(
              dp[i + 1][k][l],
              dp[i][j][l] + (stone[i][j].second + stone[i + 1][k].second) *
                                abs(stone[i][j].first - stone[i + 1][k].first));
        }
      }
      for (int k = 0; k < stone[i + 2].size(); k++) {
        for (int l = 0; l < m; l++) {
          dp[i + 2][k][l + 1] = min(
              dp[i + 2][k][l + 1],
              dp[i][j][l] + (stone[i][j].second + stone[i + 2][k].second) *
                                abs(stone[i][j].first - stone[i + 2][k].first));
        }
      }
    }
  }

  for (int i = 0; i < stone[n - 1].size(); i++) {
    for (int j = 0; j <= m; j++) {
      ans = min(ans, dp[n - 1][i][j]);
    }
  }

  printf("%d\\n", ans);

  return;
}

int main() {
  int n, m;

  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0)
      break;
    calc(n, m);
  }

  return 0;
}