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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#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, 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 MAX_Y = 55;
const int MAX_P = 5050;
vector<vector<double>> dp(MAX_Y, vector<double>(MAX_P));

int main() {
  int x, y, z;
  while (cin >> x >> y >> z && x) {
    rep(i, MAX_Y) fill(ALL(dp[i]), 0.0);

    vector<pint> g(y, make_pair(false, 0));
    vint v(x);
    rep(i, x) cin >> v[i];

    rep(i, z) {
      int n, e, a;
      cin >> n >> e >> a;

      g[n] = make_pair(e == 1, (e == 3 ? -a : a));
    }

    dp[0][0] = 1.0;
    rep(i, y) rep(j, MAX_P) {
      if (dp[i][j] == 0) {
        continue;
      }
      /*
      else if(g[i].first)
      {
              int ny = min(i + g[i].second, y);
              int np = j;

              if(ny < y && !g[ny].first)
                      np += g[ny].second;


              dp[ny][max(0, np)] += dp[i][j];
      }
      */
      else {
        rep(k, x) {
          int ny = min(i + v[k], y);
          int np = j;

          if (ny < y) {
            if (g[ny].first)
              ny += g[ny].second;
            else
              np += g[ny].second;
          }

          dp[min(ny, y)][max(0, np)] += dp[i][j] / x;
        }
      }
    }

    double res = 0;
    rep(i, MAX_P) res += dp[y][i] * i;

    cout << int(res) << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#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, 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 MAX_Y = 55;
const int MAX_P = 5050;
vector<vector<double>> dp(MAX_Y, vector<double>(MAX_P));

int main() {
  int x, y, z;
  while (cin >> x >> y >> z && x) {
    rep(i, MAX_Y) fill(ALL(dp[i]), 0.0);

    vector<pint> g(y, make_pair(false, 0));
    vint v(x);
    rep(i, x) cin >> v[i];

    rep(i, z) {
      int n, e, a;
      cin >> n >> e >> a;

      g[n] = make_pair(e == 1, (e == 3 ? -a : a));
    }

    dp[0][0] = 1.0;
    rep(i, y) rep(j, MAX_P) {
      if (dp[i][j] == 0) {
        continue;
      }
      /*
      else if(g[i].first)
      {
              int ny = min(i + g[i].second, y);
              int np = j;

              if(ny < y && !g[ny].first)
                      np += g[ny].second;


              dp[ny][max(0, np)] += dp[i][j];
      }
      */
      else {
        rep(k, x) {
          int ny = min(i + v[k], y);
          int np = j;

          if (ny < y && g[ny].first)
            ny = min(ny + g[ny].second, y);
          else if (!g[ny].first)
            np += g[ny].second;

          dp[ny][max(0, np)] += dp[i][j] / x;
        }
      }
    }

    double res = 0;
    rep(i, MAX_P) res += dp[y][i] * i;

    cout << int(res) << endl;
  }
}