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

//  Pyon-Pyon River Crossing.cpp
//  2013/01/02.

#if 1

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

const int X = 1005;
const int Y = 155;
const int MM = 80;
const int INF = 1 << 30;

int N, M;
int dp[3][X][MM];

int main() {
  for (; cin >> N >> M && (N || M);) {
    vector<vector<vint>> dp(3, vector<vint>(X, vint(MM, INF)));
    dp[0] = vector<vint>(X, vint(MM, 0));
    rep(x, X) rep(m, M) dp[1][x][m] = 0;

    vector<vector<pint>> river;

    rep(y, N) {
      int k;
      cin >> k;

      river.push_back(vector<pint>(k));

      rep(i, k) cin >> river[y][i].first >> river[y][i].second;
    }

    river.push_back(vector<pint>(X));
    river.push_back(vector<pint>(X));
    rep(i, X) river[N][i] = mp(i, 0), river[N + 1][i] = mp(i, 0);

    rep(y, N) {
      rep(i, river[y].size()) {
        rep(m, M + 1) {
          rep(j, river[y + 1].size()) {
            int d = (river[y + 1][j].second + river[y][i].second) *
                    abs(river[y + 1][j].first - river[y][i].first);
            chmin(dp[1][river[y + 1][j].first][m],
                  dp[0][river[y][i].first][m] + d);
          }

          if (m > 0)
            rep(j, river[y + 2].size()) {
              int d = (river[y + 2][j].second + river[y][i].second) *
                      abs(river[y + 2][j].first - river[y][i].first);
              chmin(dp[2][river[y + 2][j].first][m - 1],
                    dp[0][river[y][i].first][m] + d);
            }
        }
      }

      dp[0] = dp[1];
      dp[1] = dp[2];
      rep(x, X) rep(m, MM) dp[2][x][m] = INF;
    }

    int ans = INF;
    rep(x, X) rep(m, MM) chmin(ans, dp[0][x][m]);

    cout << ans << endl;
  }
}

#endif

Error: -11

Fixed submission:

//  Pyon-Pyon River Crossing.cpp
//  2013/01/02.

#if 1

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

const int X = 1005;
const int Y = 155;
const int MM = 80;
const int INF = 1 << 30;

int N, M;
int dp[3][X][MM];

int main() {
  for (; cin >> N >> M && (N || M);) {
    vector<vector<vint>> dp(3, vector<vint>(X, vint(MM, INF)));
    dp[0] = vector<vint>(X, vint(MM, 0));

    vector<vector<pint>> river;

    rep(y, N) {
      int k;
      cin >> k;

      river.push_back(vector<pint>(k));

      rep(i, k) cin >> river[y][i].first >> river[y][i].second;
    }

    river.push_back(vector<pint>(X));
    rep(i, X) river[N][i] = mp(i, 0);

    rep(y, N) {
      rep(i, river[y].size()) {
        rep(m, M + 1) {
          rep(j, river[y + 1].size()) {
            int d = (river[y + 1][j].second + river[y][i].second) *
                    abs(river[y + 1][j].first - river[y][i].first);
            chmin(dp[1][river[y + 1][j].first][m],
                  dp[0][river[y][i].first][m] + d);
          }

          if (m > 0)
            rep(j, river[y + 2].size()) {
              int d = (river[y + 2][j].second + river[y][i].second) *
                      abs(river[y + 2][j].first - river[y][i].first);
              chmin(dp[2][river[y + 2][j].first][m - 1],
                    dp[0][river[y][i].first][m] + d);
            }
        }
      }

      dp[0] = dp[1];
      dp[1] = dp[2];
      rep(x, X) rep(m, MM) dp[2][x][m] = INF;
    }

    int ans = INF;
    rep(x, X) rep(m, MM) chmin(ans, dp[0][x][m]);

    cout << ans << endl;
  }
}

#endif