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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int to_int(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string to_str(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef long long ll;

struct item {
  int kind, value;
  item() {
    kind = 0;
    value = 0;
  }
};

int main() {
  // X = roulette_num, Y = box_num, Z = event_num
  int X, Y, Z;
  while (cin >> X >> Y >> Z, X) {
    int R[4];
    item F[51];
    double dp[51][5001];
    for (int i = 0; i < X; i++) {
      cin >> R[i];
    }
    int a, b, c;
    for (int i = 0; i < Z; i++) {
      cin >> a >> b >> c;

      F[a].kind = b;
      F[a].value = c;
    }
    double prob = 1.0 / X;
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1.0;
    for (int i = 0; i < Y; i++) {
      for (int j = 0; j < 5001; j++) {
        if (dp[i][j] == 0) {
          continue;
        }
        for (int k = 0; k < X; k++) {
          int next = i + R[k];
          if (next > Y) {
            next = Y;
          }
          if (F[next].kind == 0) {
            dp[next][j] += dp[i][j] * prob;
          } else if (F[next].kind == 1) {
            next += F[next].value;
            if (next > Y) {
              next = Y;
            }
            dp[next][j] += dp[i][j] * prob;
          } else if (F[next].kind == 2) {
            int next_val = j + F[next].value;
            dp[next][next_val] += dp[i][j] * prob;
          } else if (F[next].kind == 3) {
            int next_val = j - F[next].value;
            if (next_val < 0) {
              next_val = 0;
            }
            dp[next][next_val] += dp[i][j] * prob;
          }
        }
      }
    }

    double res = 0;
    for (int i = 0; i < 5001; i++) {
      res += dp[Y][i] * i;
    }

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

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int to_int(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string to_str(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef long long ll;

struct item {
  int kind, value;
  item() {
    kind = 0;
    value = 0;
  }
};

int main() {
  // X = roulette_num, Y = box_num, Z = event_num
  int X, Y, Z;
  while (cin >> X >> Y >> Z, X) {
    int R[4];
    item F[51];
    double dp[51][5001];
    for (int i = 0; i < X; i++) {
      cin >> R[i];
    }
    int a, b, c;
    for (int i = 0; i < Z; i++) {
      cin >> a >> b >> c;

      F[a].kind = b;
      F[a].value = c;
    }
    double prob = 1.0 / X;
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1.0;
    for (int i = 0; i < Y; i++) {
      for (int j = 0; j < 5001; j++) {
        if (dp[i][j] == 0) {
          continue;
        }
        for (int k = 0; k < X; k++) {
          int next = i + R[k];
          if (next > Y) {
            next = Y;
          }
          if (F[next].kind == 0) {
            dp[next][j] += dp[i][j] * prob;
          } else if (F[next].kind == 1) {
            next += F[next].value;
            dp[next][j] += dp[i][j] * prob;
          } else if (F[next].kind == 2) {
            int next_val = j + F[next].value;
            dp[next][next_val] += dp[i][j] * prob;
          } else if (F[next].kind == 3) {
            int next_val = j - F[next].value;
            if (next_val < 0) {
              next_val = 0;
            }

            dp[next][next_val] += dp[i][j] * prob;
          }
        }
      }
    }

    double res = 0;
    for (int i = 0; i < 5001; i++) {
      res += dp[Y][i] * i;
    }

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