zoukankan      html  css  js  c++  java
  • HDU-4035-概率dp-期望-公式化简

    Maze

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
    Total Submission(s): 3149    Accepted Submission(s): 1370
    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
    3 3 1 2 1 3 0 0 100 0 0 100 3 1 2 2 3 0 0 100 0 0 100 6 1 2 2 3 1 4 4 5 4 6 0 0 20 30 40 30 50 50 70 10 20 60
     
    Sample Output
    Case 1: 2.000000 Case 2: impossible Case 3: 2.895522
     
    Source
     

         给出一颗树,起始点为1号点,接下来可以走向任意一个与当前点连接着的点,每个点有ki%的概率被杀死和ei%的概率逃生(ki+ei<=100),被杀死后将返回1号点从新开始,问逃生成功的期望步数,如果不可能逃生成功输出impossible。注意到在每个点只有三种情况死亡,逃生或者是等概率的走向其他点,走向每个点的概率就是(1-ki-ei)/ni,ni为连接点个数。设f[i]表示在i点距离逃生成功的期望步数,我们有:

      f[i]=k[i]*f[1]+e[i]*0+( (1-k[i]-e[i])/ni )*( (f[fa[i]]+1) + SUM{ f[j]+1 } )

      注意到相关联的系数只有f[1],f[fa[i]]和常数C,令f[i]=A[i]*f[1]+B[i]*f[fa[i]]+C[i],代入f[j]得到

    (1-SUM{ B[j] }*(1-ki-ei)/ni)*f[i]=(ki+SUM{A[j]}*(1-ki-ei)/ni)*f[1]+(1-ki-ei)/ni*f[fa[i]]+(1-ki-ei)/ni*(ni+SUM{C[j]} )

    令Pi=(1-ki-ei)/ni

    得到A[i]=(ki+Pi*SUM{A[j]})/(1-Pi*SUM{B[j]})

      B[i]=Pi/(1-Pi*SUM{B[j]})

      C[i]=Pi*(ni+SUM{C[j]})/(1-Pi*SUM{B[j]}) 

    然后树形dp一下由叶子向父亲不断递推得到A[1]和C[1],答案就是C[1]/(1-A[1]),如果不可能逃生成功的话,1-A[1]应该趋近于零,特判一下。

      

     1 #include<iostream>
     2 #include<cstring>
     3 #include<queue>
     4 #include<cstdio>
     5 #include<stack>
     6 #include<set>
     7 #include<map>
     8 #include<cmath>
     9 #include<ctime>
    10 #include<time.h> 
    11 #include<algorithm>
    12 using namespace std;
    13 #define mp make_pair
    14 #define pb push_back
    15 #define debug puts("debug")
    16 #define LL long long 
    17 #define pii pair<int,int>
    18 #define eps 1e-10
    19 #define inf 0x3f3f3f3f
    20 
    21 const int NN=10010;
    22 double A[NN],B[NN],C[NN],f[10010];
    23 double E[NN],K[NN];
    24 vector<int>g[NN];
    25 void dfs(int u,int fa){
    26     int ni=g[u].size();  //临接点个数 
    27     double _A=0,_B=0,_C=0;
    28     for(int i=0;i<g[u].size();++i){
    29         int v=g[u][i];
    30         if(v==fa) continue;
    31         dfs(v,u);
    32         _A+=A[v],_B+=B[v],_C+=C[v];
    33     }
    34     double P=(1.00-K[u]-E[u])/ni;
    35     A[u]=(K[u]+P*_A)/(1.00-P*_B);
    36     B[u]=P/(1.00-P*_B); if(u==1) B[u]=0;
    37     C[u]=P*(ni+_C)/(1.00-P*_B);
    38 }
    39 int main()
    40 {
    41     int t,i,j,k,n,m,u,v;
    42     scanf("%d",&t);
    43     for(int cas=1;cas<=t;++cas){
    44         scanf("%d",&n);
    45         for(i=1;i<=n;++i) g[i].clear();
    46         for(i=1;i<n;++i){
    47             scanf("%d%d",&u,&v);
    48             g[u].push_back(v);
    49             g[v].push_back(u);
    50         }
    51         for(i=1;i<=n;++i){
    52             scanf("%lf%lf",K+i,E+i);
    53             K[i]/=100;
    54             E[i]/=100;
    55         }
    56         dfs(1,-1);
    57         double ans=C[1]/(1.00-A[1]);
    58         if(fabs(1.00-A[1])<eps) printf("Case %d: impossible
    ",cas);
    59         else printf("Case %d: %.6f
    ",cas,ans);
    60     }
    61     return 0; 
    62 }
  • 相关阅读:
    python cook 整理
    Ajax
    跨站请求伪造 CSRF
    tornado web
    python hashable
    扩大了一个逻辑卷,resize2fs 保错:没有这个超级块
    linux重启后进入了救援模式,无法远程登录
    如何安装JDK以及配置win10的环境变量
    输出菱形
    6.28作业(2.使用3种方式求100以内偶数和)
  • 原文地址:https://www.cnblogs.com/zzqc/p/9024711.html
Copyright © 2011-2022 走看看