Aeroplane Chess
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2060 Accepted Submission (s): 1346
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 has six faces with equal probability to face up and the numbers on the faces is 1, 2,3,4,5,6). When Hzz was at grid I and the dice number is x, he would moves to grid i+x. Hzz finishes the game when I+x is E Qual to or greater than N.
There is 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 this there is no, or more flight lines start from the same grid.
Hzz calculate the expected dice throwing times to finish the game.
Inputthere is multiple test cases.
Each test case contains several lines.
The first line contains the integers N (1≤n≤100000) and M (0≤m≤1000).
Then M. lines follow, each line contains the integers xi,yi (1≤xi<yi≤n).
The input end with n=0, m=0.
Outputfor each test is in the input and you should output a line indicating the expected dice throwing times. Output should is rounded to 4 digits after decimal point.
Sample Input2 08 32 44 57 80 0
Sample Output1.16672.3441
Source2012 ACM/ICPC Asia Regional Jinhua Online
Recommendzhoujiaqi2010
The expectation, the inverse push.
Start with recursion, the result is crazy explosion stack ... Finally, we changed the recursive type.
#include <cstdio>#include<iostream>#include<sstream>#include<cmath>#include<cstring>#include<cstdlib>#include<string>#include<vector>#include<map>#include<Set>#include<queue>#include<stack>#include<algorithm>using namespacestd;#definell Long Long#define_cle (M, a) memset (M, (a), sizeof (m))#defineRepu (I, A, b) for (int i = A; I < b; i++)#defineMAXN 100050#defineEPS 1e-5BOOLVIS[MAXN];intFLIGHT[MAXN];intm, N;DoubleD[MAXN];intMain () { while(~SCANF ("%d%d", &n, &m) && (n +m) {_cle (flight,-1); _cle (d,0.0); intx, y; for(inti =0; I < m; i++) {scanf ("%d%d", &x, &y); FLIGHT[X]=y; } intDT; for(inti = n-1; I >=0; i--) for(intj =1; J <=6; J + +) {DT= i +J; while(Flight[dt] >0) dt =FLIGHT[DT]; D[i]+ = (D[dt] +1.0) /6.0; } printf ("%.4lf\n", d[0]); } return 0;}
View Code
HDU 4405 (Aeroplane Chess)