Hdu 4405(概率期望DP)

hdu 4405
设$dp(i)$为$i$到$n$的步数期望。

$$dp(i) = \sum \frac{1}{6}dp(i+j)$$
其中$i+j \leq n+5$或者$i+j \leq n$。如果$i$处有航线,那么直接$dp(i) = dp(lst(i))$
答案为$dp(0)​$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define db double
using namespace std;
const int MAXN = 100000 + 5;
int n, m, lst[MAXN];
db dp[MAXN];
void clean() {
ms(dp, 0), ms(lst, -1);
}
void solve() {
clean();
for (int i=1;i<=m;i++) {
int xi, yi;
scanf("%d%d", &xi, &yi);
lst[xi] = yi;
}
for (int i=n-1;i>=0;i--) {
if (lst[i]>=0) dp[i] = dp[lst[i]];
else {
for (int j=1;j<=6;j++) {
if (i+j<=n+5) dp[i] += (db)(1.0/6.0)*dp[i+j];
}
dp[i]++;
}
}
printf("%.4f\n", dp[0]);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);freopen("1.out", "w", stdout);
#endif
while (scanf("%d%d", &n, &m)==2&&(n||m)) solve();
return 0;
}

------ 本文结束 ------