Problem p00453 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <limits>
#include <vector>
using namespace std;

int main() {
  int N, M;
  while (cin >> N >> M && N != 0) {
    vector<vector<pair<int, int>>> g(N);
    for (int i = 0; i < N; i++) {
      int k;
      cin >> k;
      for (int j = 0; j < k; j++) {
        int c, d;
        cin >> c >> d;
        g[i].push_back(make_pair(c, d));
      }
    }

    vector<vector<vector<int>>> dp(N);
    for (int i = 0; i < N; i++) {
      dp[i].resize(g[i].size(),
                   vector<int>(M + 1, numeric_limits<int>::max() / 2));
    }
    for (int i = 0; i < g[0].size(); i++) {
      dp[0][i][0] = 0;
    }
    for (int i = 0; i < g[1].size(); i++) {
      dp[1][i][1] = 0;
    }

    int ans = numeric_limits<int>::max();
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < g[i].size(); j++) {
        for (int m = 0; m <= M; m++) {
          if (i + 1 == N) {
            ans = min(ans, dp[i][j][m]);
          } else {
            for (int k = 0; k < g[i + 1].size(); k++) {
              dp[i + 1][k][m] =
                  min(dp[i + 1][k][m],
                      dp[i][j][m] + (g[i][j].second + g[i + 1][k].second) *
                                        abs(g[i][j].first - g[i + 1][k].first));
            }

            if (i < N - 1 && m < M) {
              if (i + 2 == N) {
                ans = min(ans, dp[i][j][m]);
              } else {
                for (int k = 0; k < g[i + 2].size(); k++) {
                  dp[i + 2][k][m + 1] = min(
                      dp[i + 2][k][m + 1],
                      dp[i][j][m] + (g[i][j].second + g[i + 2][k].second) *
                                        abs(g[i][j].first - g[i + 2][k].first));
                }
              }
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <map>
#include <vector>
#define inf INT_MAX / 2
#define f first
#define s second
using namespace std;
typedef pair<int, int> P;
int n, m;
vector<P> riv[150];
vector<vector<vector<int>>> dp(150);
int main() {
  while (scanf("%d%d", &n, &m)) {
    if (!n && !m)
      break;
    for (int i = 0; i < n; i++) {
      int k;
      scanf("%d", &k);
      riv[i].clear();
      for (int j = 0; j < k; j++) {
        int x, d;
        scanf("%d%d", &x, &d);
        riv[i].push_back(P(x, d));
      }
    }
    for (int i = 0; i < n; i++) {
      dp[i].resize(riv[i].size(), vector<int>(m + 1, inf));
    }
    for (int i = 0; i < riv[0].size(); i++) {
      dp[0][i][0] = 0;
    }
    for (int i = 0; i < riv[1].size(); i++) {
      dp[1][i][1] = 0;
    }
    int res = inf;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < riv[i].size(); j++) {
        for (int k = 0; k <= m; k++) {
          if (i + 1 == n) {
            res = min(res, dp[i][j][k]);
          } else {
            for (int l = 0; l < riv[i + 1].size(); l++) {
              dp[i + 1][l][k] =
                  min(dp[i + 1][l][k],
                      dp[i][j][k] + (int)abs(riv[i][j].f - riv[i + 1][l].f) *
                                        (riv[i][j].s + riv[i + 1][l].s));
            }
            if (i < n - 1 && k < m) {
              if (i + 2 == n) {
                res = min(res, dp[i][j][k]);
              } else {
                for (int l = 0; l < riv[i + 2].size(); l++) {
                  dp[i + 2][l][k + 1] = min(
                      dp[i + 2][l][k + 1],
                      dp[i][j][k] + (int)abs(riv[i][j].f - riv[i + 2][l].f) *
                                        (riv[i][j].s + riv[i + 2][l].s));
                }
              }
            }
          }
        }
      }
    }
    printf("%d\\n", res);
  }
}