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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define FOREACH(i, n)                                                          \\
  for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())

#define pb push_back
#define mp make_pair

using namespace std;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;

int main() {
  ios::sync_with_stdio(false);

  int x, y, z, v[4], n, e, a;
  int dx[51], mp[51];

  while (cin >> x >> y >> z && (x > 0 || y > 0 || z > 0)) {
    REP(i, x) cin >> v[i];
    REP(i, 51) {
      dx[i] = 0;
      mp[i] = 0;
    }
    REP(i, z) {
      cin >> n >> e >> a;
      if (e == 1)
        dx[n] = a;
      else if (e == 2)
        mp[n] = a;
      else if (e == 3)
        mp[n] = -a;
    }
    double dp[51][5001];
    REP(i, y + 1) REP(j, 100 * y + 1) dp[i][j] = 0.0;
    dp[0][0] = 1.0;
    REP(i, y) {
      REP(j, 100 * y + 1) {
        if (dp[i][j] == 0)
          continue;
        REP(k, x) {
          if (i + v[k] >= y) {
            dp[y][j] += dp[i][j] / x;
            continue;
          }
          int money = j + mp[i + v[k]];
          if (money < 0)
            money = 0;
          int pos = i + v[k] + dx[i + v[k]];
          if (pos > y)
            pos = y;
          dp[pos][money] += dp[i][j] / x;
        }
      }
    }
    double ans = 0.0;
    REP(i, 100 * y + 1) { ans += dp[y][i] * i; }
    cout << (int)ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define FOREACH(i, n)                                                          \\
  for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())

#define pb push_back
#define mp make_pair

using namespace std;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;

int main() {
  ios::sync_with_stdio(false);

  int x, y, z, v[4], n, e, a;
  int dx[51], mp[51];

  while (cin >> x >> y >> z && (x > 0 || y > 0 || z > 0)) {
    REP(i, x) cin >> v[i];
    REP(i, 51) {
      dx[i] = 0;
      mp[i] = 0;
    }
    REP(i, z) {
      cin >> n >> e >> a;
      if (e == 1)
        dx[n] = a;
      else if (e == 2)
        mp[n] = a;
      else if (e == 3)
        mp[n] = -a;
    }
    double dp[51][5001];
    REP(i, y + 1) REP(j, 100 * y + 1) dp[i][j] = 0.0;
    dp[0][0] = 1.0;
    REP(i, y) {
      REP(j, 100 * y + 1) {
        if (dp[i][j] == 0)
          continue;
        REP(k, x) {
          if (i + v[k] >= y) {
            dp[y][0] += dp[i][j] / x;
          }
          int money = j + mp[i + v[k]];
          if (money < 0)
            money = 0;
          int pos = i + v[k] + dx[i + v[k]];
          if (pos > y)
            pos = y;
          dp[pos][money] += dp[i][j] / x;
        }
      }
    }
    double ans = 0.0;
    REP(i, 100 * y + 1) { ans += dp[y][i] * i; }
    cout << (int)ans << endl;
  }

  return 0;
}