HDU 4035 maze (typical tree-like expectation DP)

Source: Internet
Author: User
Tags exit in
Maze Time Limit: 2000/1000 MS (Java/others) memory limit: 65768/65768 K (Java/Others)
Total submission (s): 1677 accepted submission (s): 638
Special Judge


Problem descriptionwhen wake up, lxhgww find himself in a huge maze.

The maze consisted by N rooms and tunnels connecting these rooms. each pair of rooms is connected by one and only one path. initially, lxhgww is in room 1. each room has a dangerous trap. when lxhgww step into a room, he has a possibility to be killed and restart from Room 1. every room also has a hidden exit. each time lxhgww comes to a room, he has chance to find the exit and escape from this maze.

Unfortunately, lxhgww has no idea about the structure of the whole maze. therefore, he just chooses a tunnel randomly each time. when he is in a room, he has the same possibility to choose any tunnel connecting that room (including the tunnel he used to come to that room ).
What is the maximum CT number of tunnels he go through before he find the exit?
 
Inputfirst line is an integer T (T ≤ 30), the number of test cases.

At the beginning of each case is an integer N (2 ≤ n ≤ 10000), indicates the number of rooms in this case.

Then N-1 pairs of integers x, y (1 ≤ x, y ≤ n, x = y) are given, indicate there is a tunnel between room X and room y.

Finally, N pairs of integers KI and EI (0 ≤ Ki, EI ≤ 100, Ki + EI ≤ 100, k1 = e1 = 0) are given, indicate the percent of the possibility of been killed and exit in the ith room.
 
Outputfor each test case, output one line "case K :". K is the case ID, then the exact CT number of tunnels lxhgww go through before he exit. the answer with relative error less than 0.0001 will get accepted. if it is not possible to escape from the maze, output "impossible ".
 
Sample Input
331 21 30 0100 00 10031 22 30 0100 00 10061 22 31 44 54 60 020 3040 3050 5070 1020 60
 
Sample output
Case 1: 2.000000Case 2: impossibleCase 3: 2.895522
 
Sourcethe 36th ACM/ICPC Asia Regional Chengdu site -- online contest
Recommendlcy | we have carefully selected several similar problems for you: 4037 4036 4033 4038 question: some rooms form a tree-like maze. You start to be in the later room. You have the probability of kill each room. The probability of EI escaping from the maze. If they are neither killed nor escaped. You will randomly select a walking edge and finally ask you the expected number of edges to walk out of this maze. Idea: for the expected DP. It is easy to think that status E [I] indicates that it is currently at the I point. Expected number of edges needed to escape the maze. Then, you can write the state transition equation E [I] = Ki * E [1] + 0 * EI + (1-ei-ki) * Σ (E [J] + 1) /EDS [I]. J is the point connected to I. The number of edges where eds [I] is I. However, this equation is completely unsolvable. Because the equation has loops. However, if the amount of data is too large, the time is too complicated. So I did not do it at last. Then let's just look at the problem. There are still too few questions. I have ignored this question. The most important thing is that it is a tree rather than a graph. However, my equation does not reflect the characteristics of the tree at all. The equation should be like this if it is written by tree.
J is the son of I. FA is the father of I. Observe. You can set E [I] = A [I] * E [1] + B [I] * E [fa] + C [I]. Then
Then the code is added:
# Include <algorithm> # include <iostream> # include <string. h> # include <stdio. h> # include <math. h> using namespace STD; const int INF = 0x3f3f3f3f; const int maxn = 10010; const double EPS = 1e-10; // start-6wa. Precision must be high. Typedef long ll; struct node {int V; node * Next;} ed [maxn <1], * head [maxn]; int CNT, EDS [maxn]; double A [maxn], B [maxn], C [maxn], Ki [maxn], ei [maxn]; void Adde (INT U, int V) {ed [CNT]. V = V; Ed [CNT]. next = head [u]; head [u] = & ED [CNT ++];} void DFS (int fa, int U) {double SA, Sb, SC, mi; sa = sb = SC = 0; For (node * P = head [u]; P! = NULL; P = p-> next) {If (p-> V = FA) continue; DFS (u, p-> V ); sa + = A [p-> V]; Sb + = B [p-> V]; SC + = C [p-> V];} mi = (1-ki [u]-ei [u])/EDS [u]; A [u] = (KI [u] + Mi * Sa) /(1-mi * SB); B [u] = MI/(1-mi * SB ); c [u] = (1 + Mi * SC-ki [u]-ei [u])/(1-mi * SB);} int main () {int t, CAS = 1, n, I, U, V; scanf ("% d", & T); While (t --) {scanf ("% d", & N ); CNT = 0; memset (Head, 0, sizeof head); memset (EDS, 0, sizeof eds); for (I = 1; I <n; I ++) {scanf ("% d", & U, & V); eds [u] ++, EDS [v] ++; Adde (u, v ); ADDE (v, u) ;}for (I = 1; I <= N; I ++) {scanf ("% lf", & Ki [I], & EI [I]); Ki [I]/= 100, ei [I]/= 100;} DFS (-); printf ("case % d :", CAS ++); If (FABS (1-A [1]) <EPS) printf ("impossible \ n"); else printf ("% F \ n ", c [1]/(1-A [1]);} return 0 ;}



HDU 4035 maze (typical tree-like expectation DP)

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.