HDU 4035 Maze(树形概率DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4035

题意:一棵树,从结点1出发,在每个结点 i 都有3种可能:(1)回到结点1 , 概率 Ki;(2)结束,概率 Ei;(3)随机走一条边。(ki+ei+随机走=1) 求到结束需要走的边数的期望。

假设E[i]为点i到结束走边数的期望,则有

(以下m为点的度数)

E[i]=ki*E[1]+(1-ei-ki)/m*(E[fa[i]]+1)若i为叶子节点.

=ki*E(1)+(1-ki-ei)*E(father)+(1-ki-ei)

E[i]=ki*E[1]+(1-ei-ki)/m*(E[fa[i]]+1)+(1-ei-ki)/m*(Sum(E[son[i]]+1))i不为叶子节点

=ki*E(1)+(1-ki-ei)/m *E(father)+(1-ki-ei)/m*SUM(E(child))+(1-ki-ei)  作为1式

我们发现,这样求非常麻烦,若是n小一点大可用高斯消元求解,可这题的n为10000,无法用高斯消元。

对于每个E[i],我们令E[i]=Ai*(E[1])+Bi*(E[fa[i]])+Ci

E[child]=Aj*E[1]+Bj*E[i]+Cj

Sum(E[child])=Sum(Aj*E[1]+Bj*E[i]+Cj)

带入1式:ki*E(1)+(1-ki-ei)/m *E(father)+(1-ki-ei)/m*Sum(Aj*E[1]+Bj*E[i]+Cj)+(1-ki-ei)

可得:(ki+(1-ki-ei)/m*SUM(Aj))*E(1)+(1-ki-ei)/m *E(father)+(1-ki-ei+(1-ki-ei)/m*SUM(cj))

与刚才的E[i]=Ai*(E[1])+Bi*(E[fa[i]])+Ci对比一下发现:

Ai=(ki+(1-ki-ei)/m*SUM(Aj))

Bi=(1-ki-ei)/m

Ci=(1-ki-ei+(1-ki-ei)/m*SUM(cj))

对于叶子节点,有

Ai=ki

Bi=1-ki-ei

Ci=1-ki-ei

倒推即可,还有,

E(1)=A1*E(1)+B1*0+C1

E(1)=C1/(1-A1)

若是上述式子中的分母出现0则无解。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<cmath>
 5 #include<algorithm>
 6 const double eps=1e-9;
 7 int tot,go[500005],first[500005],next[500005];
 8 double a[500005],b[500005],c[500005],k[500005],e[500005];
 9 int n,du[500005];
10 void insert(int x,int y){tot++;go[tot]=y;next[tot]=first[x];first[x]=tot;}
11 void add(int x,int y){insert(x,y);insert(y,x);}
12 bool dfs(int x,int fa){
13     bool Isleave=1;
14     double tmp=0;
15     a[x]=k[x];
16     b[x]=c[x]=(1-k[x]-e[x]);
17     b[x]/=du[x];
18     for (int i=first[x];i;i=next[i]){
19         int pur=go[i];
20         if (pur==fa) continue;
21         Isleave=0;
22         if (!dfs(pur,x)) return false;
23         a[x]+=a[pur]*(1-k[x]-e[x])/du[x];
24         c[x]+=c[pur]*(1-k[x]-e[x])/du[x];
25         tmp+=(b[pur])*(1-k[x]-e[x])/du[x];
26     }
27     if (fabs(tmp-1)<=eps) return false;
28     a[x]/=(1-tmp);
29     b[x]/=(1-tmp);
30     c[x]/=(1-tmp);
31     return true;
32 }
33 int main(){
34     int T,Tcase=0;
35     scanf("%d",&T);
36     while (T--){
37         Tcase++;
38         tot=0;
39         memset(first,0,sizeof first);
40         memset(du,0,sizeof du);
41         scanf("%d",&n);
42         for (int i=1;i<n;i++){
43             int x,y;
44             scanf("%d%d",&x,&y);
45             add(x,y);
46             du[x]++;
47             du[y]++;
48         }
49         for (int i=1;i<=n;i++){
50             scanf("%lf%lf",&k[i],&e[i]);
51             k[i]/=100;e[i]/=100;
52         }
53         printf("Case %d: ",Tcase);
54         if (dfs(1,0)&&fabs(1-a[1])>eps){
55             printf("%.6f\n",c[1]/(1-a[1]));
56         }
57         else{
58             printf("impossible\n");
59         }
60     }
61 }
时间: 2024-10-16 13:14:56

HDU 4035 Maze(树形概率DP)的相关文章

hdu 4219, 树形概率DP

题意: 给定一颗树,每机在区间 [0,L] 中选择.求最终形成的树上任意两点间距离不超过S的概率. 解决: dp[以i为根][以j为叶子节点到i的最远距离] 当j*2<=s的时候,表示这个子树上的最长链不可能超过s,那么 可以任意取值就是当前的概率,但是为了保证j是精确的,所以要 减去距离小于等于j-1的概率: 当j*2>s的时候,这个子树必定有且仅有一个链的长度是s,那么 枚举该链,让其他链的长度任意取值,所有情况之和就是概率. 待补.

HDU 4652 Dice (概率DP)

Dice Problem Description You have a dice with m faces, each face contains a distinct number. We assume when we tossing the dice, each face will occur randomly and uniformly. Now you have T query to answer, each query has one of the following form: 0

HDU 3366 Passage (概率DP)

Passage Problem Description Bill is a millionaire. But unfortunately he was trapped in a castle. There are only n passages to go out. For any passage i (1<=i<=n), Pi (0<=Pi<=1) denotes the probability that Bill will escape from this castle saf

【bzoj3566】[SHOI2014]概率充电器 树形概率dp

题目描述 著名的电子产品品牌 SHOI 刚刚发布了引领世界潮流的下一代电子产品——概率充电器:“采用全新纳米级加工技术,实现元件与导线能否通电完全由真随机数决定!SHOI 概率充电器,您生活不可或缺的必需品!能充上电吗?现在就试试看吧!”SHOI 概率充电器由 n-1 条导线连通了 n 个充电元件.进行充电时,每条导线是否可以导电以概率决定,每一个充电元件自身是否直接进行充电也由概率决定.随后电能可以从直接充电的元件经过通电的导线使得其他充电元件进行间接充电.作为 SHOI 公司的忠实客户,你无

[ACM] HDU 4576 Robot (概率DP,滚动数组)

Robot Problem Description Michael has a telecontrol robot. One day he put the robot on a loop with n cells. The cells are numbered from 1 to n clockwise. At first the robot is in cell 1. Then Michael uses a remote control to send m commands to the ro

[ACM] hdu 4405 Aeroplane chess (概率DP)

Aeroplane chess Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal probability to face up and the number

HDU 4050 wolf5x (概率DP 求期望)

题意:有N个格子,1~N,起点在0,每个格子有一个状态(0,1,2,3),每次可以跨[a,b]步, 问走完N个格子需要步数的期望,每次尽量走小的步数,即尽量走a步,不能则走a+1,-- 状态0意味着你不能踏进对应的网格. 状态1意味着你可以??步入网格用你的左腿. 状态2意味着你可以??步入网格用你的右腿. 状态3意味着你可以进入网格用任何你的腿,而接下来的步骤中,您可以使用任何的腿;即你不需要遵循上述规则. 思路:借鉴了各路大神的思想理解了下. dp[i][j] :表示走到第 i 个格子在 j

[ACM] hdu 3853 LOOPS (概率DP,递推)

LOOPS Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help her friend Madoka save the world. But because of the plot of the Boss Incubator, she is trapped in a labyrinth called LOOPS. The planform of the

HDU 4035:Maze(概率DP)

http://acm.split.hdu.edu.cn/showproblem.php?pid=4035 Maze 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