HDU 4405 Aeroplane chess (概率dp)

Problem Description

Hzz 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,3,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.

Input

There 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.

Output

For each test case in the input, you should output a line indicating the expected dice throwing times. Output should be rounded to 4 digits after decimal point.

Sample Input

2 08 32 44 57 80 0

Sample Output

1.16672.3441

题意: 从0到比n大的点,每一步扔塞子,决定走什么距离,,不过有的地方有飞机,可以直接坐飞机,

思路:概率dp

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<stack>#include<vector>#include<set>#include<map>#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define eps 1e-8typedef __int64 ll;#define fre(i,a,b) for(i = a; i <b; i++)#define free(i,b,a) for(i = b; i >= a;i–)#define mem(t, v) memset ((t) , v, sizeof(t))#define ssf(n)scanf("%s", n)#define sf(n)scanf("%d", &n)#define sff(a,b) scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pfprintf#define bugpf("Hi\n")using namespace std;#define INF 0x3f3f3f3f#define N 100005int to[N],n,m;double dp[N];int main(){int i,j;while(~sff(n,m),n+m){mem(dp,0);mem(to,0);int a,b;while(m–){sff(a,b);to[a]=b;}free(i,n-1,0){if(to[i]){dp[i]=dp[to[i]]; //直接坐飞机continue;}dp[i]=1;fre(j,1,7)dp[i]+=dp[i+j]*1.0/6;}pf("%.4f\n",dp[0]);}return 0;}

爱情不是避难所,想进去避难的话,是会被赶出来的。

HDU 4405 Aeroplane chess (概率dp)

相关文章:

你感兴趣的文章:

标签云: