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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int i, j, k, l;
  int a[10][150][78], x[11][150], d[10][150], m, n;
  int mn;
  while (cin >> n >> m && n + m) {
    memset(x, -1, sizeof(x));
    memset(d, -1, sizeof(d));
    for (i = 0; i < n; i++) {
      cin >> k;
      for (j = 0; j < k; j++) {
        int t, u;
        cin >> t >> u;
        t--;
        x[j][i] = t;
        d[j][i] = u;
      }
    }
    memset(a, -1, sizeof(a));
    for (i = 0; x[i][0] != -1; i++)
      a[i][0][0] = 0;
    if (m) {
      for (i = 0; x[i][1] != -1; i++)
        a[i][1][1] = 0;
    }
    for (i = 0; i < n - 2; i++) {
      for (j = 0; x[j][i] != -1; j++) {
        for (k = 0; k < n / 2 + 3; k++) {
          if (a[j][i][k] != -1) {
            for (l = 0; x[l][i + 1] != -1; l++) {
              if (a[l][i + 1][k] == -1 ||
                  a[l][i + 1][k] > a[j][i][k] + (d[j][i] + d[l][i + 1]) *
                                                    abs(x[j][i] - x[l][i + 1]))
                a[l][i + 1][k] = a[j][i][k] + (d[j][i] + d[l][i + 1]) *
                                                  abs(x[j][i] - x[l][i + 1]);
            }
            if (k != m) {
              for (l = 0; x[l][i + 2] != -1; l++) {
                if (a[l][i + 2][k + 1] == -1 ||
                    a[l][i + 2][k + 1] >
                        a[j][i][k] + (d[j][i] + d[l][i + 2]) *
                                         abs(x[j][i] - x[l][i + 2]))
                  a[l][i + 2][k + 1] =
                      a[j][i][k] +
                      (d[j][i] + d[l][i + 2]) * abs(x[j][i] - x[l][i + 2]);
              }
            }
          }
        }
      }
    }
    mn = -1;
    for (i = 0; x[i][n - 2] != -1; i++) {
      for (j = 0; j < n / 2 + 3; j++) {
        if (a[i][n - 2][j] != -1) {
          if (j == m) {
            for (k = 0; x[k][n - 1] != -1; k++) {
              if (a[k][n - 1][j] == -1 ||
                  a[k][n - 1][j] >
                      a[i][n - 2][j] + (d[i][n - 2] + d[k][n - 1]) *
                                           abs(x[i][n - 2] - x[k][n - 1]))
                a[k][n - 1][j] =
                    a[i][n - 2][j] + (d[i][n - 2] + d[k][n - 1]) *
                                         abs(x[i][n - 2] - x[k][n - 1]);
            }
          } else if (mn == -1 || mn > a[i][n - 2][j])
            mn = a[i][n - 2][j];
        }
      }
    }
    for (i = 0; x[i][n - 1] != -1; i++) {
      for (j = 0; j < n / 2 + 3; j++) {
        if (a[i][n - 1][j] != -1) {
          if (mn == -1 || mn > a[i][n - 1][j])
            mn = a[i][n - 1][j];
        }
      }
    }
    cout << mn << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int i, j, k, l;
  int a[10][150][78], x[10][150], d[10][150], m, n;
  int mn;
  while (cin >> n >> m && n + m) {
    memset(x, -1, sizeof(x));
    memset(d, -1, sizeof(d));
    for (i = 0; i < n; i++) {
      cin >> k;
      for (j = 0; j < k; j++) {
        int t, u;
        cin >> t >> u;
        t--;
        x[j][i] = t;
        d[j][i] = u;
      }
    }
    memset(a, -1, sizeof(a));
    for (i = 0; x[i][0] != -1; i++)
      a[i][0][0] = 0;
    if (m) {
      for (i = 0; x[i][1] != -1; i++)
        a[i][1][1] = 0;
    }
    for (i = 0; i < n - 2; i++) {
      for (j = 0; x[j][i] != -1; j++) {
        for (k = 0; k < n / 2 + 3; k++) {
          if (a[j][i][k] != -1) {
            for (l = 0; x[l][i + 1] != -1; l++) {
              if (a[l][i + 1][k] == -1 ||
                  a[l][i + 1][k] > a[j][i][k] + (d[j][i] + d[l][i + 1]) *
                                                    abs(x[j][i] - x[l][i + 1]))
                a[l][i + 1][k] = a[j][i][k] + (d[j][i] + d[l][i + 1]) *
                                                  abs(x[j][i] - x[l][i + 1]);
            }
            if (k != m) {
              for (l = 0; x[l][i + 2] != -1; l++) {
                if (a[l][i + 2][k + 1] == -1 ||
                    a[l][i + 2][k + 1] >
                        a[j][i][k] + (d[j][i] + d[l][i + 2]) *
                                         abs(x[j][i] - x[l][i + 2]))
                  a[l][i + 2][k + 1] =
                      a[j][i][k] +
                      (d[j][i] + d[l][i + 2]) * abs(x[j][i] - x[l][i + 2]);
              }
            }
          }
        }
      }
    }
    mn = -1;
    for (i = 0; x[i][n - 2] != -1; i++) {
      for (j = 0; j < n / 2 + 3; j++) {
        if (a[i][n - 2][j] != -1) {
          if (j == m) {
            for (k = 0; x[k][n - 1] != -1; k++) {
              if (a[k][n - 1][j] == -1 ||
                  a[k][n - 1][j] >
                      a[i][n - 2][j] + (d[i][n - 2] + d[k][n - 1]) *
                                           abs(x[i][n - 2] - x[k][n - 1]))
                a[k][n - 1][j] =
                    a[i][n - 2][j] + (d[i][n - 2] + d[k][n - 1]) *
                                         abs(x[i][n - 2] - x[k][n - 1]);
            }
          } else if (mn == -1 || mn > a[i][n - 2][j]) {
            mn = a[i][n - 2][j];
          }
        }
      }
    }
    for (i = 0; x[i][n - 1] != -1; i++) {
      for (j = 0; j < n / 2 + 3; j++) {
        if (a[i][n - 1][j] != -1) {
          if (mn == -1 || mn > a[i][n - 1][j])
            mn = a[i][n - 1][j];
        }
      }
    }
    cout << mn << endl;
  }
  return 0;
}