Hdu4418--time Travel

Source: Internet
Author: User

Time travel Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1739 Accepted Submission (s): 394


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 and the time machine. The time machine can take agent K-some point (0 to n-1) on the timeline and when he gets to the end of the time line he Would come back (for example, there is 4 time points, agent K would go in the this is 0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1, ... .). But when agent K gets into the time machine he finds it had broken, which make the time machine can ' t stop (Damn it!). Fortunately, the time machine could get recovery and stop for a few minutes when agent K arrives at a time point, if the Tim E Point He just arrive are his destination, he's ll go and finish his mission, or the time machine would break again. The time machine had probability pk% to recover after passing K time points and K can is no more than M. We guarantee the sum of Pk is (SUM (PK) (1 <= k <= M) ==100). Now we know Agent K'll appear at the Point X (D-is theDirection 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 that time line D would 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 fi Nish his mission.
If Finishing He mission is impossible output "impossible!" (no quotes) instead.

Inputthere is a integer t (t <=) indicating the cases you has to solve. The first line of all test case is five integers N, M, Y, X. D (0< n,m <=, 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 are impossible output one line "Impossible!"
(no quotes) instead.

Sample Input
24 2 0 1 050 504 1 0 2 1100

Sample Output
8.142.00

Source2012 ACM/ICPC Asia Regional Hangzhou Online
recommendliuyiding | We have carefully selected several similar problems for you:5153 5152 5151 5150 5149

Statistic | Submit | Discuss | Note


The probability DP, set Dp[i] indicates the desired number of steps to reach the endpoint at point I


Dp[i] = P1 * (dp[i + 1] + 1) + P2 * (Dp[i + 2] + 2) + ... + pm * (dp[i + m] + m);

Of course dp[e] = 0;

But because there are 2 directions, we have to turn the reverse state into a positive direction, that is, the road symmetry, 012345-"0123456789

such as 4 go to 3 to become 6 go to 7, the direction is fixed, and then you can establish a linear equation group to solve, note that some points to not, this must first use BFS treatment, by the way to each of the points numbered, and then the Gaussian elimination of the line


/************************************************************************* > File Name:hdu4418.cpp > Author: ALex > Mail: [email protected] > Created time:2014 December 29 Monday 17:01 42 seconds ******************************* /#include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const int N = 210;const double EPS  = 1e-12;double sum_p;double mat[n][n], x[n];d ouble p[n];bool free_x[n];int ord[n];int N, M, S, E, Cnt;int Gauss () {int col, K, Equ, Var, max_r;int free_index, free_num;equ = Cnt;var = cnt;k = col = 0;for (int i = 0; i < var; ++i) {Free_x[i] = True;x[i] = 0;} For (K < equ && Col < var; ++k, ++col) {max_r = k;for (int i = k + 1; i < equ; ++i) {if (Fabs (Mat[i][col) )-Fabs (Mat[max_r][col]) &Gt EPS) {max_r = i;}} if (max_r! = k) {for (int j = k; J < var + 1; ++j) {swap (mat[k][j], mat[max_r][j]);}} if (Fabs (Mat[k][col]) <= EPS) {--k;continue;} for (int i = k + 1; i < equ; ++i) {double TMP = Mat[i][col]/mat[k][col];for (int j = col; J < var + 1; ++j) {mat[i][ J]-= tmp * MAT[K][J];}}} Determine the condition of the solution for (int i = k; i < equ; ++i) {if (Fabs (Mat[i][var]) > EPS) {return 0;}}  if (K < Var) {for (int i = k-1; I >= 0; i) {free_num = 0;for (int j = 0; J < var; ++j) {if (Fabs (mat[i][j)) > EPS && free_x[j]) {free_num++;free_index = j;}} if (Free_num > 1) {continue;} Double tmp = mat[i][var];for (int j = 0; J < var; ++j) {if (Fabs (Mat[i][j]) > EPS && J! = Free_index) {tmp-= MAT[I][J] * x[j];}} Free_x[free_index] = False;x[free_index] = Tmp/mat[i][free_index];} return var-k;} for (int i = var-1; I >= 0; i) {double tmp = mat[i][var];for (int j = i + 1; j < var; ++j) {if (Fabs (Mat[i][j]) &G T EPS) {tmp-= x[j] * mat[i][j];}} X[i] = Tmp/mat[i][i];}return 1;} void BFs () {cnt = 0;queue <int> Qu;memset (Ord,-1, sizeof (ORD)); Ord[s] = Cnt++;while (!qu.empty ()) {Qu.pop ();} Qu.push (s), while (!qu.empty ()) {int u = qu.front (), Qu.pop (), for (int i = 1; I <= m; ++i) {if (P[i] <= eps) {continue;} int v = (i + u)% n;if (ord[v] = = 1) {Ord[v] = Cnt++;qu.push (v);}}} void Build () {for (int i = 0; i < n; ++i) {if (ord[i] = = 1) {continue;} if (i = = E | | i = = (n-e)% n) {Mat[ord[i]][ord[i]] = 1;mat[ord[i]][cnt] = 0;continue;} Mat[ord[i]][ord[i]] = 1.0;for (int j = 1; j <= m; ++j) {if (ord[(i + j)% n] = =-1) {continue;} mat[ord[i]][ord[(i + j)% n]]-= p[j];} MAT[ORD[I]][CNT] = sum_p;}} int main () {int T, d;scanf ("%d", &t), while (t--) {sum_p = 0;memset (Mat, 0, sizeof (MAT)), scanf ("%d%d%d%d%d", &n, &am P;m, &e, &s, &d); n = 2 * n-2;for (int i = 1; I <= m; ++i) {scanf ("%lf", &p[i]);p [i]/= 100.0;sum_p + = I * p[i];} if (s = = E | | n = = 1) {printf ("0.00\n"); continue;} if (D > 0) {s = (n/a)% n;} BFS (); if (ord[e] = =-1 &&amP ord[(N-E)% n] = =-1) {printf ("Impossible!\n"); continue;} Build (); if (! Gauss ()) {printf ("Impossible!\n"); continue;} printf ("%.2f\n", X[ord[s]]);} return 0;}


Hdu4418--time Travel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.