Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1551 accepted submission (s): 1063
Problem descriptionhzz loves aeroplane chess very much. the chess map contains N + 1 grids labeled from 0 to n. hzz starts at grid 0. for each step he throws a dice (a dice have six faces with equal probability to face up and the numbers on the faces are 1, 2, 4, 5, 6 ). when hzz is at grid I and the dice number is X, he will moves to grid I + X. hzz finishes the game when I + X is equal to or greater than N.
There are also m flight lines on the chess map. the I-th flight line can help hzz fly from grid XI to Yi (0 <xi <Yi <= N) without throwing the dice. if there is another flight line from Yi, hzz can take the flight line continuously. it is granted that there is no two or more flight lines start from the same grid.
Please help hzz calculate the expected dice throwing times to finish the game.
Inputthere are multiple test cases.
Each test case contains several lines.
The first line contains two integers n (1 ≤ n ≤ 100000) and M (0 ≤ m ≤ 1000 ).
Then M lines follow, each line contains two integers Xi, Yi (1 ≤ xi <Yi ≤ n ).
The input end with n = 0, m = 0.
Outputfor each test case in the input, you shocould output a line indicating the expected dice throwing times. Output shocould be rounded to 4 digits after decimal point.
Sample Input
2 08 32 44 57 80 0
Sample output
1.16672.3441
Question: There are n grids. The number of throws you throw is the number of cells you move at a time. There are m flight channels that allow you to directly access
Fly to the yige. Ask what you expect to reach the end.
Train of Thought: DP [I] indicates the expectation of the end point in the I-th lattice.
When I is not the starting point of the flight channel: DP [I] = 1.0 + (DP [I + 1] + dp [I + 2] + dp [I + 3] + dp [I + 4] + dp [I + 5] + dp [I + 6]) /6.0;
When I is the starting point of the flight channel: DP [XI] = DP [Yi] (because Xi can be directly connected to Yi and does not need to throw );
#include <iostream>#include <cstring>#include <cstdio>using namespace std;const int N=100050;int n,m,mark,x,y,pre[N];double dp[N];void input(){ memset(pre,-1,sizeof(pre)); for(int i=0; i<m; i++) { scanf("%d %d",&x,&y); pre[x]=y; } memset(dp,0,sizeof(dp));}void solve(){ for(int i=n-1;i>=0;i--) { if(pre[i]!=-1) dp[i]=dp[pre[i]]; else { for(int j=1;j<=6;j++) dp[i]+=dp[i+j]; dp[i]=dp[i]/6.0+1.0; } } printf("%.4lf\n",dp[0]);}int main(){ while(scanf("%d %d",&n,&m)!=EOF) { if(n==0 && m==0) break; input(); solve(); } return 0;}
HDU 4405 aeroplane chess (probability DP + expectation)