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

#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
#define for_each(it, a)                                                        \\
  for (__typeof a.begin() it = a.begin(); it != a.end(); it++)
template <class T> inline void chmin(T &a, T b) {
  if (b < a)
    a = b;
}

int n, m;
struct edge {
  int pos, dang;
  vector<int> value;
  edge(int pos, int dang) : pos(pos), dang(dang), value((n + 1) / 2 + 1, INF){};
};
int main() {

  while (cin >> n >> m, n) {
    vector<vector<edge>> info(n);

    for (int i = 0; i < n; i++) {
      int k;
      cin >> k;
      for (int j = 0; j < k; j++) {
        int t, d;
        cin >> t >> d;
        info[i].push_back(edge(t, d));
      }
    }
    for_each(it, info[0]) it->value[m] = 0;
    for_each(it, info[1]) it->value[m - 1] = 0;
    int ret = INF;
    for (int i = 0; i < n; i++) {
      for_each(now, info[i]) {
        for (int j = m; j >= 0; j--) { // ジャンプできる残り回数
          int &cost = now->value[j], dang = now->dang, pos = now->pos;
          if (cost == INF)
            continue;
          if (j > 0) { // ジャンプしてみる
            if (i + 2 >= n)
              chmin(ret, cost);
            else
              for_each(next, info[i + 2])
                  chmin(next->value[j - 1],
                        cost + (dang + next->dang) * abs(pos - next->pos));
          }
          if (i + 1 >= n)
            chmin(ret, cost);
          else
            for_each(next, info[i + 1])
                chmin(next->value[j],
                      cost + (dang + next->dang) * abs(pos - next->pos));
        }
      }
    }
    cout << ret << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define fr first
#define sc second
#define INF INT_MAX
typedef pair<int, int> Pi;
typedef pair<Pi, pair<Pi, int>> P;
struct Edge {
  int to, denger;
  Edge() {}
  Edge(int to, int denger) : to(to), denger(denger){};
};
int n, m;
int m_cost[151][11][(151 + 1) / 2 + 1];
vector<vector<Edge>> info;
int Dijkstra();
int main() {
  while (cin >> n >> m && n || m) {
    info.resize(n);
    for (int i = 0, k; i < n; i++) {
      cin >> k;
      for (int j = 0, t, d; j < k; j++) {
        cin >> t >> d;
        info[i].push_back(Edge(t, d));
      }
    }
    cout << Dijkstra() << endl;
    info.clear();
  }
}
int Dijkstra() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < (n + 1) / 2 + 1; k++) {
        m_cost[i][j][k] = INF;
      }
    }
  }
  priority_queue<P, vector<P>, greater<P>> que;
  for (int i = 0, l = info[0].size(); i < l; i++) {
    que.push(P(Pi(0, m), make_pair(Pi(0, info[0][i].to), info[0][i].denger)));
    m_cost[0][i][m] = 0;
  }
  if (m != 0) {
    for (int i = 0, l = info[1].size(); i < l; i++) {
      que.push(
          P(Pi(0, m - 1), make_pair(Pi(1, info[1][i].to), info[1][i].denger)));
      m_cost[1][i][m - 1] = 0;
    }
  }
  while (!que.empty()) {
    P p = que.top();
    int den = p.fr.fr, limit = p.fr.sc, slip = p.sc.sc;
    Pi pt = Pi(p.sc.fr.fr, p.sc.fr.sc);
    que.pop();
    if (pt.fr + 1 >= n)
      return den;
    for (int j = 1; j < 3; j++) {
      if (j == 2 && limit == 0)
        break;
      if (j == 2 && pt.fr + 1 == n)
        break;
      if (j == 2 && pt.fr + 2 == n)
        return den;
      if (j == 1 && pt.fr + 1 == n)
        return den;
      for (int i = 0, l = info[pt.fr + j].size(); i < l; i++) {
        int next_now = info[pt.fr + j][i].to;
        int next_den = info[pt.fr + j][i].denger;
        int hmhm = den + (slip + next_den) * abs(next_now - pt.sc);
        if (hmhm < m_cost[pt.fr + j][i][limit - (j - 1)]) {
          m_cost[pt.fr + j][i][limit - (j - 1)] = hmhm;
          que.push(P(Pi(hmhm, limit - (j - 1)),
                     make_pair(Pi(pt.fr + j, next_now), next_den)));
        }
      }
    }
  }
}