(概率+树形)dp HDU 4035 Maze

来源:互联网 发布:程序员帅哥 编辑:程序博客网 时间:2024/05/16 05:54

Maze

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1300    Accepted Submission(s): 478
Special Judge


Problem Description
When 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 expect number of tunnels he go through before he find the exit?
 

Input
First 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.
 

Output
For each test case, output one line “Case k: ”. k is the case id, then the expect 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
 

Source
The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  4037 4036 4031 4033 4038 


题意:给出一颗数,一个人从1开始想要逃迷宫,每在一个迷宫,他有一定的概率被杀死,回到1,有一定的概率,逃走,如果上两个事情不发生,那么他能随机到与他的房间相邻的房间。问逃离迷宫的期望步数。

思路:我们用dp[i] 表示从i逃出去的期望步数,那么dp[i] = e[i]+k[i]*dp[1]+(1-e[i]-k[i])/deg[i]*sigma(dp[son]+1)+(1-e[i]-k[i])/deg[i]*(dp[father]+1) ,  那么我们把dp写成dp[i] = A[i] + B[i]*k[i]+C[i]*dp[fa] , 那么dp[son]和dp[fa]就能合并,
dp[x] = A[x]+B[x]*dp[1]+C[x]*dp[fa]+(1-e[x]-k[x])/deg[x]*sigma(A[y]+B[y]*dp[1]+C[y]*dp[x]); 注意根没有父亲
我们从儿子往上更新,求出所有的A,B,C最后dp[1] = A[1]+B[1]*dp[1]+0*dp[fa] => dp[1] = A[1]/(1-B[1])

代码:
#include<iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<math.h>using namespace std;#define eps 1e-9const int maxn = 10000+5;struct Node{    int y;    Node *next;}edge[2*maxn] , *first[maxn];int n , m , sum[maxn];double E[maxn] , K[maxn] , A[maxn] , B[maxn] , C[maxn];void add(int x,int y){    edge[++m].y = y;    edge[m].next = first[x];    first[x] = &edge[m];}void dfs(int x,int fa){    Node * p = first[x];    A[x] = 1-K[x] , B[x] = K[x] , C[x] = (1-E[x]-K[x])/sum[x];    if (x==1) C[x] = 0;    bool isleaf = true;    double e = E[x] , k = K[x];    double c = (1-e-k)/sum[x];    int y;    double Cy = 0;    while (p)    {        y = p->y;        if (y!=fa)        {            isleaf = false;            dfs(y,x);            A[x] += c*A[y];            B[x] += c*B[y];            Cy += c*C[y];        }        p = p->next;    }    A[x] /= (1-Cy);    B[x] /= (1-Cy);    C[x] /= (1-Cy);}void init(){    m = 0;    memset(sum,0,sizeof(sum));    memset(first,0,sizeof(first));    memset(edge,0,sizeof(edge));}void input(){    for (int i = 0 ; i < n-1 ; ++i)    {        int x , y;        scanf("%d%d",&x,&y);        add(x,y);        add(y,x);        ++sum[x] , ++sum[y];    }    for (int i = 1 ; i <= n ; ++i)    {        scanf("%lf%lf",K+i,E+i);        K[i] *= 0.01;        E[i] *= 0.01;    }}void solve(){    dfs(1,-1);    double ans = A[1]/(1-B[1])-1;    if (fabs(1-B[1]) < eps) printf("impossible\n");    else if (fabs(ans) < 1e-6) printf("0.000000\n");    else printf("%.6lf\n",ans);}int main(){    int T; cin>>T;    for (int Cas = 1 ; Cas <= T ; ++Cas)    {        scanf("%d",&n);        init();        input();        printf("Case %d: ",Cas);        solve();    }}
0 0
原创粉丝点击