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

#include <cstdlib>
#include <iostream>
using namespace std;
const int INF = (1 << 29);

int main() {
  int n, m;
  while (cin >> n >> m && (n || m)) {
    int dp[150][10][80], a[150][10], x[150][10];
    for (int i = 0; i < 150; i++) {
      for (int j = 0; j < 10; j++) {
        for (int k = 0; k < 80; k++)
          dp[i][j][k] = INF;
        a[i][j] = INF;
      }
    }
    for (int i = 0; i < n; i++) {
      int k;
      cin >> k;
      for (int j = 0; j < k; j++) {
        int b, c;
        cin >> b >> c;
        x[i][j] = b;
        a[i][j] = c;
      }
    }
    for (int i = 0; i < 10; i++) {
      dp[0][i][0] = 0;
      if (m != 1)
        dp[1][i][1] = 0;
    }
    for (int i = 0; i < n - 1; i++) {
      for (int l = 0; l <= m; l++) {
        for (int j = 0; j < 10; j++) {
          if (a[i][j] == INF)
            continue;
          for (int k = 0; k < 10; k++) {
            if (a[i + 1][k] == INF)
              continue;
            dp[i + 1][k][l] =
                min(dp[i + 1][k][l],
                    dp[i][j][l] +
                        (a[i][j] + a[i + 1][k]) * (abs(x[i + 1][k] - x[i][j])));
          }
          if (i < n - 2 && l != m) {
            for (int k = 0; k < 10; k++) {
              if (a[i + 2][k] == INF)
                continue;
              dp[i + 2][k][l + 1] =
                  min(dp[i + 2][k][l + 1],
                      dp[i][j][l] + (a[i][j] + a[i + 2][k]) *
                                        (abs(x[i + 2][k] - x[i][j])));
            }
          }
        }
      }
    }

    int ans = INF;
    for (int j = 0; j <= m; j++)
      for (int k = 0; k < 10; k++)
        ans = min(ans, dp[n - 1][k][j]);
    for (int j = 0; j < m; j++)
      for (int k = 0; k < 10; k++)
        ans = min(ans, dp[n - 2][k][j]);

    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdlib>
#include <iostream>
using namespace std;
const int INF = (1 << 21);

int main() {
  int n, m;
  while (cin >> n >> m && (n || m)) {
    int dp[150][10][80], a[150][10];
    for (int i = 0; i < 150; i++) {
      for (int j = 0; j < 10; j++) {
        for (int k = 0; k < 80; k++)
          dp[i][j][k] = INF;
        a[i][j] = INF;
      }
    }
    for (int i = 0; i < n; i++) {
      int k;
      cin >> k;
      for (int j = 0; j < k; j++) {
        int b, c;
        cin >> b >> c;
        b--;
        a[i][b] = c;
      }
    }
    for (int i = 0; i < 10; i++)
      dp[0][i][0] = 0;

    for (int i = 0; i < n - 1; i++) {
      for (int l = 0; l <= m; l++) {
        for (int j = 0; j < 10; j++) {
          if (a[i][j] == INF)
            continue;
          for (int k = 0; k < 10; k++) {
            if (a[i + 1][k] == INF)
              continue;
            dp[i + 1][k][l] =
                min(dp[i + 1][k][l],
                    dp[i][j][l] + (a[i][j] + a[i + 1][k]) * (abs(k - j)));
          }
          if (i < n - 2 && l != m) {
            for (int k = 0; k < 10; k++) {
              if (a[i + 2][k] == INF)
                continue;
              dp[i + 2][k][l + 1] =
                  min(dp[i + 2][k][l + 1],
                      dp[i][j][l] + (a[i][j] + a[i + 2][k]) * (abs(k - j)));
            }
          }
        }
      }
    }

    int ans = INF;
    for (int j = 0; j <= m; j++)
      for (int k = 0; k < 10; k++)
        ans = min(ans, dp[n - 1][k][j]);

    cout << ans << endl;
  }
  return 0;
}