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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;
int n, m, c, a, b;
int main() {
  ios::sync_with_stdio(false);
  while (cin >> n >> m, n | m) {
    vector<vector<pair<int, int>>> v(n);
    for (int i = 0; i < n; i++) {
      cin >> c;
      for (int j = 0; j < c; j++) {
        cin >> a >> b;
        v[i].push_back(make_pair(a, b));
      }
    }
    vector<vector<vector<int>>> d(n);
    for (int i = 0; i < n; i++)
      d[i] = vector<vector<int>>(v[i].size(), vector<int>(m + 1, 999999999));
    priority_queue<tuple<int, int, int, int>> que; // (dist, y, x, z)
    for (int i = 0; i <= 1 && i <= m; i++) {
      for (int j = 0; j < v[i].size(); j++) {
        d[i][j][i] = 0;
        que.push(make_tuple(0, i, j, i));
      }
    }
    while (!que.empty()) {
      tuple<int, int, int, int> t1 = que.top();
      que.pop();
      int a1 = -get<0>(t1), b1 = get<1>(t1), c1 = get<2>(t1), d1 = get<3>(t1);
      for (int i = 1; i <= 2 && b1 + i < n; i++) {
        if (i == 2 && d1 == m)
          continue;
        for (int j = 0; j < v[b1 + i].size(); j++) {
          int s = abs(v[b1][c1].first - v[b1 + i][j].first) *
                  (v[b1][c1].second + v[b1 + i][j].second);
          if (d[b1 + i][j][d1 + i - 1] > a1 + s) {
            d[b1 + i][j][d1 + i - 1] = a1 + s;
            que.push(
                make_tuple(-d[b1 + i][j][d1 + i - 1], b1 + i, j, d1 + i - 1));
          }
        }
      }
    }
    int ret = 999999999;
    for (int i = 0; i <= 1 && i <= m; i++) {
      for (int j = 0; j < v[n - i - 1].size(); j++) {
        for (int k = 0; k <= m - i; k++) {
          ret = min(ret, d[n - i - 1][j][k]);
        }
      }
    }
    cout << ret << endl;
  }
}

Error: WA

Fixed submission:

#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>

using namespace std;

int n, m, f1, f2, r[150];

vector<pair<int, int>> R[150];
int D[150][10][76];

int distance(int x1, int c1, int x2, int c2) {
  int sums = R[x1][c1].second + R[x2][c2].second;
  int dist = R[x1][c1].first + R[x2][c2].first;

  return sums * abs(dist);
}

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

    if (n == 0 && m == 0)
      break;

    for (int i = 0; i < n; i++)
      R[i].clear();

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

      for (int j = 0; j < r[i]; j++) {
        scanf("%d", &f1);
        scanf("%d", &f2);

        R[i].push_back(make_pair(f1, f2));
      }
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < r[i]; j++) {
        for (int k = 0; k <= m; k++) {
          if (i == 0) {
            D[i][j][k] = 0;
          } else {
            D[i][j][k] = 999999999;
          }
        }
      }
    }

    priority_queue<tuple<int, int, int, int>, vector<tuple<int, int, int, int>>,
                   greater<tuple<int, int, int, int>>>
        que;

    for (int i = 0; i < R[0].size(); i++) {
      que.push(make_tuple(0, 0, 0, i));
    }

    while (!que.empty()) {
      tuple<int, int, int, int> state = que.top();
      que.pop();

      int dist = get<0>(state);
      int jump = get<1>(state);
      int high = get<2>(state);
      int node = get<3>(state);

      if (high + 1 != n) {
        for (int i = 0; i < r[high + 1]; i++) {
          int dist2 = dist + distance(high, node, high + 1, i);

          if (D[high + 1][node][jump] > dist2) {
            D[high + 1][node][jump] = dist2;

            que.push(make_tuple(dist2, jump, high + 1, i));
          }
        }

        if (high + 2 != n && jump != m) {
          for (int i = 0; i < r[high + 2]; i++) {
            int dist2 = dist + distance(high, node, high + 2, i);

            if (D[high + 2][node][jump + 1] > dist2) {
              D[high + 2][node][jump + 1] = dist2;

              que.push(make_tuple(dist2, jump + 1, high + 1, i));
            }
          }
        }
      }
    }

    int ret = 999999999;

    for (int i = 0; i < r[n - 1]; i++) {
      ret = min(ret, D[n - 1][i][m]);
    }

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

  return 0;
}