Hdu 4035 Maze (probabilistic DP)

Source: Internet
Author: User
Tags exit in

Portal: http://acm.hdu.edu.cn/showproblem.php?pid=4035 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 are connected by one and only one path. Initially, LXHGWW is in the 1. Each is a dangerous trap. When the LXHGWW step into a, he had a possibility to being killed and restart from the 1. Every also has a hidden exit. Each time LXHGWW comes to a, he had chance to find the exit and escape from this maze.

Unfortunately, Lxhgww have no idea about the structure of the whole maze. Therefore, he just chooses a tunnel randomly each time. When he was in a, he had the same possibility to choose any tunnel connecting the (including the tunnel he used To the come to the).
What is the expect number of tunnels he go through before he find the exit?

Inputfirst line was an integer T (t≤30), the number of test cases.

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

Then N-1 pairs of integers x, Y (1≤x, Y≤n, x≠y) were given, indicate there is a tunnel between the class X and Class 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 T He possibility of been killed and exit in the ith.

Outputfor Each test case, the output one line "Case K:". K is the case ID and then the expect number of tunnels lxhgww go through before he exit. The answer with relative error less than 0.0001 would get accepted. If It is not a possible to escape from the maze, output "impossible".

Sample Input331 21 30 0100 00 10031 22 30 0100 00 10061 22 31 44 54 60 020 3040 3050 5070 1020 60

Sample outputcase 1:2.000000case 2:impossiblecase 3:2.895522
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;Const intn=1e4+5;Const Doubleeps=1e-9;intT,n;intDeg[n];DoubleA[n],b[n],c[n];DoubleK[n],e[n];intHead[n],num_edge;structedge{intV,NXT;} Edge[n<<1];inlinevoidAdd_edge (intUintv) {edge[++num_edge].v=v; EDGE[NUM_EDGE].NXT=Head[u]; Head[u]=Num_edge;}BOOLDfsintUintFA) {    Doublem=Deg[u]; A[u]=K[u]; B[u]=(1-k[u]-e[u])/m; C[u]=1-k[u]-E[u]; Doubletmp=1, ratio=B[u];  for(intI=head[u],v;i;i=edge[i].nxt) {v=edge[i].v; if(v==FA)Continue; if(!DFS (v,u))return false; TMP-=ratio*B[v]; A[u]+=ratio*A[v]; C[u]+=ratio*C[v]; }    if(Fabs (TMP) <=EPS)return false; A[u]/=tmp; B[u]/=tmp; C[u]/=tmp; return true;}intTaskID;intMain () {scanf ("%d",&T);  while(t--) {memset (deg,0,sizeof(deg)); memset (Head,0,sizeof(head)); Num_edge=0; scanf ("%d",&N);  for(intI=1, a,b;i<n;++i) {scanf ("%d%d",&a,&b); ++deg[a],++Deg[b];            Add_edge (A, b);        Add_edge (B,a); }         for(intI=1; i<=n;++i) {scanf ("%LF%LF", k+i,e+i); K[i]/= -, e[i]/= -; } cout<<" Case"<<++taskid<<": "; if(Dfs (1,1) &&fabs (1-a[1]) >EPS) cout<<c[1]/(1-a[1]) <<'\ n'; Elseputs ("Impossible"); }        return 0;}

Hdu 4035 Maze (probabilistic 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.