Hdu 3853 LOOPS (expected)
The expectation of power consumed from [1, 1] to [r, c] is calculated. The power consumed by each step is 2, from [I, j] to [I, c, j], [I, j + 1], [I + 1] [j] probability.
Dp [I] [j] indicates the expectation of power consumption from [I, j] to [r, c]. It is known that the final dp [r] [c] = 0, then, push back.
It is hard to think that when the probability of in-situ is 1, it cannot go through [r, c], and the result in the state transition equation is INF, which is in conflict with the requirements of the question.
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include // # define LL _ int64 # define LL long # define eps 1e-9 # define PI acos (-1.0) using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 4010; double dp [1010] [1010]; double a [1010] [3010]; int main () {int n, m; while (~ Scanf (% d, & n, & m) {for (int I = 1; I <= n; I ++) {for (int j = 1; j <= 3 * m; j ++) scanf (% lf, & a [I] [j]);} memset (dp, 0.0, sizeof (dp )); dp [n] [m] = 0; int flag = 1; for (int I = n; I> = 1; I --) {for (int j = m; j> = 1; j --) {if (I = n & j = m) continue; if (fabs (1.0-a [I] [(J-1) * 3 + 1]) <eps) // key {continue ;} dp [I] [j] = (dp [I] [j + 1] * a [I] [3 * (J-1) + 2] + dp [I + 1] [j] * a [I] [j * 3] + 2)/(1.0-a [I] [(J-1) * 3 + 1]);} if (flag = 0) break;} printf (%. 3lf, dp [1] [1]);} return 0 ;}