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

#include <cassert>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <queue>

#define N 200
#define ACOL 100
#define JUMP 100

const int infty = 1 << 29;

using namespace std;

struct state {
  int r, c, cost, left;
  state(int r, int c, int cost, int left)
      : r(r), c(c), cost(cost), left(left) {}
  bool operator<(const state &t) const { return cost > t.cost; }
};

int dijkstra(int n, int m, const vector<vector<int>> &C,
             const vector<vector<int>> &D) {
  static bool vis[N][ACOL][JUMP];
  static int A[N][ACOL][JUMP];
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < ACOL; ++j)
      for (int k = 0; k < JUMP; ++k) {
        vis[i][j][k] = false;
        A[i][j][k] = infty;
      }
  priority_queue<state> qs;
  qs.push(state(0, 0, 0, m));

  while (!qs.empty()) {
    state s = qs.top();
    qs.pop();
    if (s.r >= n + 1) {
      continue;
    }
    vis[s.r][s.c][s.left] = true;
    for (int k = 1; k <= 2; ++k) {
      for (int i = 0; i < C[s.r + k].size(); ++i) {
        int nxr = s.r + k, nxc = i,
            nxcost = s.cost + (D[s.r][s.c] + D[nxr][nxc]) *
                                  abs(C[s.r][s.c] - C[nxr][nxc]),
            nxj = s.left - (k - 1);
        if (s.r == 0 || nxr >= n + 1)
          nxcost = s.cost;
        if (nxj < 0)
          continue;
        state next(nxr, nxc, nxcost, nxj);
        if (!vis[nxr][nxc][nxj] && A[nxr][nxc][nxj] > nxcost) {
          A[nxr][nxc][nxj] = nxcost;
          qs.push(next);
        }
      }
    }
  }
  int ret = infty;
  for (int i = 0; i < JUMP; ++i) {
    for (int j = 0; j < ACOL; ++j) {
      ret = min(A[n + 1][j][i], min(A[n + 2][j][i], ret));
    }
  }
  return ret;
}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<vector<int>> D(n + 3);
    D[0].push_back(0);
    D[n + 1].push_back(0);
    D[n + 2].push_back(0);
    vector<vector<int>> C(n + 3);
    C[0].push_back(0);
    C[n + 1].push_back(0);
    C[n + 2].push_back(0);
    for (int i = 0; i < n; ++i) {
      int k;
      cin >> k;
      for (int j = 0; j < k; ++j) {
        int col, danger;
        cin >> col >> danger;
        C[i + 1].push_back(col);
        D[i + 1].push_back(danger);
      }
    }
    cout << dijkstra(n, m, C, D) << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cassert>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <queue>

#define N 200
#define ACOL 100
#define JUMP 100

const int infty = 1 << 29;

using namespace std;

struct state {
  int r, c, cost, left;
  state(int r, int c, int cost, int left)
      : r(r), c(c), cost(cost), left(left) {}
  bool operator<(const state &t) const { return cost > t.cost; }
};

int dijkstra(int n, int m, const vector<vector<int>> &C,
             const vector<vector<int>> &D) {
  static bool vis[N][ACOL][JUMP];
  static int A[N][ACOL][JUMP];
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < ACOL; ++j)
      for (int k = 0; k < JUMP; ++k) {
        vis[i][j][k] = false;
        A[i][j][k] = infty;
      }
  priority_queue<state> qs;
  qs.push(state(0, 0, 0, m));

  while (!qs.empty()) {
    state s = qs.top();
    qs.pop();
    if (s.r >= n + 1) {
      continue;
    }
    vis[s.r][s.c][s.left] = true;
    for (int k = 1; k <= 2; ++k) {
      for (int i = 0; i < C[s.r + k].size(); ++i) {
        int nxr = s.r + k, nxc = i,
            nxcost = s.cost + (D[s.r][s.c] + D[nxr][nxc]) *
                                  abs(C[s.r][s.c] - C[nxr][nxc]),
            nxj = s.left - (k - 1);
        if (s.r == 0 || nxr >= n + 1)
          nxcost = s.cost;
        if (nxj < 0)
          continue;
        state next(nxr, nxc, nxcost, nxj);
        if (!vis[nxr][nxc][nxj] && A[nxr][nxc][nxj] > nxcost) {
          A[nxr][nxc][nxj] = nxcost;
          qs.push(next);
        }
      }
    }
  }
  int ret = infty;
  for (int i = 0; i < JUMP; ++i) {
    for (int j = 0; j < ACOL; ++j) {
      ret = min(A[n + 1][j][i], min(A[n + 2][j][i], ret));
    }
  }
  return ret;
}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (cin.eof() || n == 0 && m == 0)
      break;
    vector<vector<int>> D(n + 3);
    D[n + 1].push_back(0);
    D[n + 2].push_back(0);
    vector<vector<int>> C(n + 3);
    C[n + 1].push_back(0);
    C[n + 2].push_back(0);
    for (int i = 0; i < n; ++i) {
      int k;
      cin >> k;
      for (int j = 0; j < k; ++j) {
        int col, danger;
        cin >> col >> danger;
        C[i + 1].push_back(col);
        D[i + 1].push_back(danger);
      }
    }
    cout << dijkstra(n, m, C, D) << endl;
  }
  return 0;
}