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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

#define f first
#define s second
#define MAX_N 155
#define INF 100000000000000

using namespace std;

typedef long long ll;

typedef pair<int, int> P;

int main(void) {

  ll n, m, k, a, b;

  while (cin >> n >> m, n | m) {
    vector<P> v[MAX_N];

    for (int i = 1; i <= n; i++) {
      cin >> k;
      for (int j = 0; j < k; j++) {
        cin >> a >> b;
        v[i].push_back(make_pair(a, b));
      }
    }

    ll dp[MAX_N][11][80];

    for (int i = 0; i < MAX_N; i++)
      for (int j = 0; j < 11; j++)
        for (int k = 0; k < 80; k++)
          dp[i][j][k] = INF;

    for (int i = 0; i < 11; i++)
      for (int j = 0; j < 80; j++)
        dp[0][i][j] = dp[1][i][j] = 0;

    for (int i = 2; i <= n; i++) {
      for (int j = 0; j < v[i].size(); j++) {
        for (int l = m; l >= 0; l--) {
          for (int k = 0; k < v[i - 1].size(); k++) {
            dp[i][j][l] =
                min(dp[i][j][l],
                    dp[i - 1][k][l] + (v[i][j].s + v[i - 1][k].s) *
                                          abs(v[i][j].f - v[i - 1][k].f));
          }
          if (l < m) {
            if (i == 2)
              dp[i][j][l] = 0;
            for (int p = 0; p < v[i - 2].size(); p++)
              dp[i][j][l] =
                  min(dp[i][j][l],
                      dp[i - 2][p][l + 1] + (v[i][j].s + v[i - 2][p].s) *
                                                abs(v[i][j].f - v[i - 2][p].f));
            // dp[i][j][l]=min(dp[i][j][l],dp[i][j][l+1]);
          }
        }
      }
    }

    ll ans = INF;
    /*
    for(int i=0;i<v[n].size();i++){
      for(int j=0;j<=m;j++){
      ans=min(ans,dp[n][i][j]);
      if(j>0)ans=min(ans,dp[n-1][i][j]);
      }
    }
    */
    for (int i = 0; i < v[n].size(); i++)
      ans = min(ans, dp[n][i][0]);
    if (m >= 1)
      for (int i = 0; i < v[n - 1].size(); i++)
        ans = min(ans, dp[n - 1][i][1]);

    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
#define MAX_V 155
#define INF 1000000

using namespace std;

struct node {
  int line, row;
};
struct edge {
  int cost;
  node now, to;
};

typedef pair<int, int> P;

vector<edge> G[MAX_V];
int n, m;

void solved(void) {
  int dp[155][11][80];

  for (int i = 0; i < 101; i++)
    for (int j = 0; j <= 10; j++)
      for (int k = 0; k < 80; k++)
        dp[i][j][k] = INF;

  for (int i = 0; i < 11; i++)
    dp[0][i][m] = 0;

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < G[i].size(); j++) {

      edge e = G[i][j];
      for (int k = 0; k <= m; k++) {

        if (i + 1 == e.to.line)
          dp[e.to.line][e.to.row][k] =
              min(dp[e.to.line][e.to.row][k],
                  dp[e.now.line][e.now.row][k] + e.cost);
        if (i + 2 == e.to.line && k != 0)
          dp[e.to.line][e.to.row][m - k] =
              min(dp[e.to.line][e.to.row][m - k],
                  dp[e.now.line][e.now.row][m - k + 1] + e.cost);
      }
    }
  }
  int ans = INF;
  for (int k = 0; k < 11; k++)
    for (int j = 0; j <= m; j++)
      ans = min(ans, dp[n + 1][k][j]);

  cout << ans << endl;
}

int main(void) {
  int k, x, y;
  int graph[MAX_V][1005];

  while (cin >> n >> m, n | m) {

    for (int i = 0; i < MAX_V; i++)
      G[i].clear();

    for (int i = 0; i < MAX_V; i++)
      for (int j = 0; j < 1005; j++)
        graph[i][j] = 0;

    for (int i = 1; i <= n; i++) {
      cin >> k;

      for (int j = 0; j < k; j++) {
        cin >> x >> y;
        graph[i][x] = y;
      }
    }

    edge e;
    for (int i = 0; i <= n; i++) {
      e.now.line = i;
      e.now.row = e.to.row = 0;
      for (int j = 0; j < 1005; j++) {

        if (graph[i][j] > 0 || i == 0) {
          int row1 = 0, row2 = 0;
          for (int l = 0; l < 1005; l++) {

            if (i == 0 || i >= n) {

              if (graph[i + 1][l] > 0 || i + 1 == n + 1) {
                e.to.line = i + 1;
                e.cost = 0;
                e.to.row = row1;
                row1++;
                G[i].push_back(e);
              }
              if (graph[i + 2][l] > 0 || i + 2 == n + 1) {
                e.to.line = i + 2;
                e.cost = 0;
                e.to.row = row2;
                row2++;
                G[i].push_back(e);
              }
              continue;
            }

            if (graph[i + 1][l] > 0) {
              e.to.line = i + 1;
              e.to.row = row1;
              row1++;
              e.cost = (graph[i][j] + graph[i + 1][l]) * abs(l - j);
              G[i].push_back(e);
            }
            if (graph[i + 2][l] > 0) {
              e.to.line = i + 2;
              e.to.row = row2;
              row2++;
              e.cost = (graph[i][j] + graph[i + 2][l]) * abs(l - j);
              G[i].push_back(e);
            }
          }
          if (i)
            e.now.row++;
          if (i == 0)
            break;
        }
      }
    }
    solved();
  }
  return 0;
}