HDU 4035 maze (probability 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): 1713 accepted submission (s): 659
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
 


DP to find the expected question.
Question:
There are n rooms connected by n-1 tunnels, which actually forms a tree,
Starting from node 1, there are three possibilities for each node I:
1. Killed, return to node 1 (probability is Ki)
2. Find the exit and exit the maze (probability is EI)
3. There are m edges connected to the vertex and a Random Edge is taken.
The expected value of the number of edges to walk out of the maze.

Set E [I] to represent the expected number of edges to walk out of the maze at node I. E [1] is the request.

Leaf node:
E [I] = Ki * E [1] + EI * 0 + (1-ei EI) * (E [Father [I] + 1 );
= Ki * E [1] + (1-0000ei) * E [Father [I] + (1-0000ei );

Non-leaf node: (m is the number of edges connected to the node)
E [I] = Ki * E [1] + EI * 0 + (1-ei EI) /M * (E [Father [I] + 1 + Σ (E [child [I] + 1 ));
= Ki * E [1] + (1-0000ei)/m * E [Father [I] + (1-0000ei)/m * Σ (E [child [I]) + (1-0000ei );

Set each node: E [I] = ai * E [1] + bi * E [Father [I] + ci;

For non-leaf node I, if J is set to the child node of I, then
Σ (E [child [I]) = Σ E [J]
= Σ (AJ * E [1] + Bj * E [Father [J] + cj)
= Σ (AJ * E [1] + Bj * E [I] + cj)
The preceding formula is used.
(1-(1-0000ei)/m * Σ BJ) * E [I] = (KI + (1-0000ei)/m * Σ AJ) * E [1] + (1-0000ei) /M * E [Father [I] + (1-0000ei) + (1-0000ei)/m * Σ CJ;
Therefore
Ai = (KI + (1-0000ei)/m * Sigma AJ)/(1-(1-0000ei)/m * Σ BJ );
Bi = (1-0000ei)/M/(1-(1-0000ei)/m * Σ BJ );
Ci = (1-digit EI) + (1-digit EI)/m * Sigma CJ)/(1-(1-digit EI)/m * Sigma BJ );

For leaf nodes
Ai = Ki;
Bi = 1-ki-EI;
Ci = 1-ki-EI;

Starting from the leaf node until A1, B1, and c1 are calculated;

E [1] = A1 * E [1] + B1 * 0 + C1; // because the root node has no parent node
So
E [1] = C1/(1-A1 );
If A1 approaches 1, there is no solution...


#include<stdio.h>#include<math.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<iostream>#include<vector>using namespace std;#define N 10005#define LL __int64vector<int>g[N];const double eps=1e-10;double a[N],b[N],c[N],e[N],k[N];bool dfs(int t,int pre){    int i,m=g[t].size();    a[t]=k[t];    b[t]=(1-k[t]-e[t])/m;    c[t]=1-k[t]-e[t];    double tmp=0;    for(i=0;i<m;i++)    {        int v=g[t][i];        if(v==pre)            continue;        if(!dfs(v,t))            return false;        a[t]+=a[v]*(1-k[t]-e[t])/m;        c[t]+=c[v]*(1-k[t]-e[t])/m;        tmp+=b[v]*(1-k[t]-e[t])/m;    }    if(fabs(1-tmp)<eps)        return false;    a[t]/=(1-tmp);    b[t]/=(1-tmp);    c[t]/=(1-tmp);    return true;}int main(){    int T,i,n,u,v,cnt=1;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        for(i=0;i<=n;i++)            g[i].clear();        for(i=1;i<n;i++)        {            scanf("%d%d",&u,&v);            g[u].push_back(v);            g[v].push_back(u);        }        for(i=1;i<=n;i++)        {            scanf("%lf%lf",&k[i],&e[i]);            k[i]/=100;            e[i]/=100;        }        printf("Case %d: ",cnt++);        if(dfs(1,-1)&&fabs(1-a[1])>eps)        {            double ans=c[1]/(1-a[1]);            printf("%.8f\n",ans);        }        else            printf("impossible\n");    }    return 0;}




HDU 4035 maze (probability 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.