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

// Solution for aoj:0530 Pyon-Pyon River Crossing
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int INF = 10000000000, MAX_N = 150;
typedef pair<int, int> P;
int n, m, ans;
int dp[MAX_N + 3][1001][76];
P stone, stone2;
vector<P> Stone[151]; // first:??????second:???????????????
int main() {
  while (cin >> n >> m) {
    if (!n && !m)
      return 0;
    for (int i = 1; i <= n; i++)
      Stone[i].clear();
    for (int i = 1; i <= n; i++) {
      int a, b, c;
      cin >> c;
      for (int j = 0; j < c; j++) {
        cin >> a >> b;
        Stone[i].push_back(P(a, b));
      }
    }
    for (int i = 2; i <= n; i++) {
      for (int j = 1; j <= 1000; j++) {
        for (int k = 0; k <= m; k++) {
          dp[i][j][k] = 0;
        }
      }
    }
    for (int i = 0; i < Stone[2].size(); i++) {
      stone = Stone[2][i];
      dp[2][stone.first][m] = INF;
    }
    for (int i = 3; i <= n; i++) {
      for (int j = 0; j < Stone[i].size(); j++) {
        stone = Stone[i][j];
        for (int k = 0; k <= 75; k++)
          dp[i][stone.first][k] = INF;
      }
    }
    for (int i = 1; i < n; i++) {
      for (int j = 0; j < Stone[i].size(); j++) {
        stone = Stone[i][j];
        for (int k = 0; k < Stone[i + 1].size(); k++) {
          stone2 = Stone[i + 1][k];
          for (int l = m; l >= m - i / 2; l--) {
            if (l < 0)
              break;
            dp[i + 1][stone2.first][l] =
                min(dp[i + 1][stone2.first][l],
                    (stone.second + stone2.second) *
                            abs(stone.first - stone2.first) +
                        dp[i][stone.first][l]);
          }
        }
        for (int k = 0; k < Stone[i + 2].size(); k++) {
          if (i + 2 > n)
            break;
          stone2 = Stone[i + 2][k];
          for (int l = m - 1; l >= m - (i + 2) / 2; l--) {
            if (l < 0)
              break;
            dp[i + 2][stone2.first][l] =
                min(dp[i + 2][stone2.first][l],
                    (stone.second + stone2.second) *
                            abs(stone.first - stone2.first) +
                        dp[i][stone.first][l + 1]);
          }
        }
      }
    }
    ans = INF;
    for (int i = 0; i < Stone[n].size(); i++) {
      stone = Stone[n][i];
      for (int j = 0; j <= m; j++)
        ans = min(dp[n][stone.first][j], ans);
    }
    for (int i = 0; i < Stone[n - 1].size(); i++) {
      stone = Stone[n - 1][i];
      for (int j = 1; j <= m; j++)
        ans = min(dp[n - 1][stone.first][j], ans);
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

// Solution for aoj:0530 Pyon-Pyon River Crossing
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int INF = 100000000, MAX_N = 150;
typedef pair<int, int> P;
int n, m, ans;
int dp[MAX_N + 20][1010][80];
P stone, stone2;
vector<P> Stone[151]; // first:??????second:???????????????
int main() {
  while (cin >> n >> m) {
    if (!n && !m)
      return 0;
    for (int i = 1; i <= n; i++)
      Stone[i].clear();
    for (int i = 1; i <= n; i++) {
      int a, b, c;
      cin >> c;
      for (int j = 0; j < c; j++) {
        cin >> a >> b;
        Stone[i].push_back(P(a, b));
      }
    }
    for (int i = 2; i <= n; i++) {
      for (int j = 1; j <= 1000; j++) {
        for (int k = 0; k <= m; k++) {
          dp[i][j][k] = 0;
        }
      }
    }
    for (int i = 0; i < Stone[2].size(); i++) {
      stone = Stone[2][i];
      dp[2][stone.first][m] = INF;
    }
    for (int i = 3; i <= n; i++) {
      for (int j = 0; j < Stone[i].size(); j++) {
        stone = Stone[i][j];
        for (int k = 0; k <= m; k++)
          dp[i][stone.first][k] = INF;
      }
    }
    for (int i = 1; i < n; i++) {
      for (int j = 0; j < Stone[i].size(); j++) {
        stone = Stone[i][j];
        for (int k = 0; k < Stone[i + 1].size(); k++) {
          stone2 = Stone[i + 1][k];
          for (int l = m; l >= m - (i + 1) / 2; l--) {
            if (l < 0)
              break;
            if (l != m)
              dp[i + 1][stone2.first][l] = min(dp[i + 1][stone2.first][l],
                                               dp[i + 1][stone2.first][l + 1]);
            dp[i + 1][stone2.first][l] =
                min(dp[i + 1][stone2.first][l],
                    (stone.second + stone2.second) *
                            abs(stone.first - stone2.first) +
                        dp[i][stone.first][l]);
          }
        }
        for (int k = 0; k < Stone[i + 2].size(); k++) {
          stone2 = Stone[i + 2][k];
          for (int l = m - 1; l >= m - (i + 2) / 2; l--) {
            if (l < 0)
              break;
            if (l != m)
              dp[i + 1][stone2.first][l] = min(dp[i + 2][stone2.first][l],
                                               dp[i + 2][stone2.first][l + 1]);
            dp[i + 2][stone2.first][l] =
                min(dp[i + 2][stone2.first][l],
                    (stone.second + stone2.second) *
                            abs(stone.first - stone2.first) +
                        dp[i][stone.first][l + 1]);
          }
        }
      }
    }
    ans = INF;
    for (int i = 0; i < Stone[n].size(); i++) {
      stone = Stone[n][i];
      ans = min(dp[n][stone.first][0], ans);
    }
    for (int i = 0; i < Stone[n - 1].size(); i++) {
      if (m == 0)
        break;
      stone = Stone[n - 1][i];
      if (dp[n - 1][stone.first][1])
        ans = min(dp[n - 1][stone.first][1], ans);
    }
    cout << ans << endl;
  }
}