Time travel
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 517 accepted submission (s): 80
Problem description
Agent K is one of the greatest agents in a secret organization called men in black. once he needs to finish a mission by traveling through time with the time machine. the time machine can take Agent K to some point (0 to n-1) on the timeline and when he gets to the end of the time line he will come back (for example, there are 4 time points, Agent K will go in this way 0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1,...). But when Agent K gets into the time machine he finds it has broken, which make the time machine can't stop (damn it !). Fortunately, the time machine may get recovery and stop for a few minutes when Agent K arrives at a time point, if the time point he just arrive is his destination, he'll go and finish his mission, or the time machine will break again. the time machine has probability PK % to recover after passing K time points and K can be no more than M. we guarantee the sum of PK is 100 (sum (PK) (1 <= k <= m) = 100 ). now we know Agent K will appear at the point X (D is the direction of the Time Machine: 0 represents going from the start of the timeline to the end, on the contrary 1 represents going from the end. if X is the start or the end point of the time line D will be-1. agent K want to know the expectation of the amount of the time point he need to pass before he arrive at the point y to finish his mission.
If finishing his mission is impossible output "impossible! "(No quotes) instead.
Inputthere is an integer T (t <= 20) indicating the cases you have to solve. the first line of each test case are five integers n, m, Y, X. D (0 <n, m <= 100, 0 <= X, Y <100 ). the following m non-negative integers represent pk in percentile.
Outputfor each possible scenario, output a floating number with 2 digits after decimal point
If finishing his mission is impossible output one line "impossible! "
(No quotes) instead.
Sample input2 4 2 0 1 0 50 4 1 0 2 100
Sample output8.14 2.00
Source2012 ACM/ICPC Asia Regional Hangzhou online
Recommendliuyiding
Converts N points into 2 * N-2 points.
Then the Gaussian elimination method is used to solve the probability DP.
/* HDU 4118 question: there is a digital axis with a start point and an end point. Someone can walk for 1, 2, 3 ...... In step m, there is a probability in each case. At first, there is a direction. When you go to the beginning, return and ask how many expected steps are reached. Obvious Gaussian expectation Problem */ # Include <Stdio. h> # Include <Iostream> # Include <Algorithm> # Include < String . H> # Include <Queue> # Include <Math. h>Using Namespace STD; # Define EPS 1e-9 Const Int Maxn = 220 ; Double A [maxn] [maxn], X [maxn]; // Returns the matrix on the left of the equation and the value on the right of the equation. Int Equ, VaR ; // Number of equations and number of unknowns Int Gauss (){ Int I, J, K, Col, max_r; For (K = 0 , Col = 0 ; K <equ & Col < VaR ; K ++, Col ++ ) {Max_r = K; For (I = K + 1 ; I <equ; I ++ ) If (FABS (A [I] [col])> FABS (A [max_r] [col]) max_r = I; If (FABS (A [max_r] [col]) <EPS) Return 0 ; If (K! = Max_r ){ For (J = Col; j < VaR ; J ++ ) Swap (A [k] [J], a [max_r] [J]); swap (X [K], X [max_r]);} X [k] /= A [k] [col]; For (J = Col + 1 ; J < VaR ; J ++) A [k] [J]/= A [k] [col]; A [k] [col] = 1 ; For (I = 0 ; I <equ; I ++ ) If (I! = K) {x [I] -= X [k] * A [I] [k]; For (J = Col +1 ; J < VaR ; J ++) A [I] [J]-= A [k] [J] * A [I] [col]; A [I] [col] = 0 ;}} Return 1 ;} Int Num [maxn]; Double P [maxn]; Int CNT; Int N, N;// N = 2 * N-2 Int M; Void BFS ( Int S) {memset (Num, - 1 , Sizeof (Num); queue < Int > Que; CNT = 0 ; Num [s] = CNT ++; Que. Push (s ); While (! Que. Empty ()){ Int T = Que. Front (); Que. Pop (); For ( Int I = 1 ; I <= m; I ++ ){ If (FABS (P [I]) <EPS) Continue ; // This is very important. This thought cannot be achieved. Int Temp = (t + I) % N; If (Num [temp] =- 1 ) {Num [temp] = CNT ++ ; Que. Push (temp );}}}} Int Main (){ // Freopen ("in.txt", "r", stdin ); // Freopen ("out.txt", "W", stdout ); Int S, E; Int D; Int T; scanf ( " % D " ,& T ); While (T -- ) {Scanf ( " % D " , & N, & M, & E, & S ,& D ); For ( Int I = 1 ; I <= m; I ++) {scanf ( " % Lf " , & P [I]); P [I]/= 100 ;} If (E = s) // This feature is required. Otherwise, n = 1 may be divided by 0, re {Printf ( " 0.00 \ n " ); Continue ;} N = 2 * (N- 1 ); If (D = 1 ) S = N- S; BFS (s ); If (Num [e] =- 1 & Num [n-e] =- 1 ) {Printf ( " Impossible! \ N " ); Continue ;} Equ = VaR = CNT; memset (, 0 , Sizeof (A); memset (X, 0 , Sizeof (X )); For ( Int I =0 ; I <n; I ++ ) If (Num [I]! =- 1 ){ If (I = E | I = N- E) {A [num [I] [num [I] = 1 ; X [num [I] = 0 ; Continue ;} A [num [I] [num [I] =1 ; For ( Int J = 1 ; J <= m; j ++ ){ Int T = (I + J) % N; If (Num [T]! =- 1 ) {A [num [I] [num [T] -= P [J]; X [num [I] + = J *P [J] ;}} If (Gauss () printf ( " %. 2lf \ n " , X [num [s]); Else Printf ( " Impossible! \ N " );} Return 0 ;}