Maze
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2012 Accepted Submission(s): 802
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
The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest
这道题我是看过题解才会做的。
真的是好题。
有时候,概率DP的转移方程写出来后,发现不能简单的递推得到,含有其他未知数,怎么办?
比如,y=f(x),要求出y,就要知道x,但是x的值又和y有关,这个时候可以列出几个方程,
消去未知数。
题解:
转自:http://blog.csdn.net/morgan_xww/article/details/6776947
- /**
- dp求期望的题。
- 题意:
- 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树,
- 从结点1出发,开始走,在每个结点i都有3种可能:
- 1.被杀死,回到结点1处(概率为ki)
- 2.找到出口,走出迷宫 (概率为ei)
- 3.和该点相连有m条边,随机走一条
- 求:走出迷宫所要走的边数的期望值。
- 设 E[i]表示在结点i处,要走出迷宫所要走的边数的期望。E[1]即为所求。
- 叶子结点:
- E[i] = ki*E[1] + ei*0 + (1-ki-ei)*(E[father[i]] + 1);
- = ki*E[1] + (1-ki-ei)*E[father[i]] + (1-ki-ei);
- 非叶子结点:(m为与结点相连的边数)
- E[i] = ki*E[1] + ei*0 + (1-ki-ei)/m*( E[father[i]]+1 + ∑( E[child[i]]+1 ) );
- = ki*E[1] + (1-ki-ei)/m*E[father[i]] + (1-ki-ei)/m*∑(E[child[i]]) + (1-ki-ei);
- 设对每个结点:E[i] = Ai*E[1] + Bi*E[father[i]] + Ci;
- 对于非叶子结点i,设j为i的孩子结点,则
- ∑(E[child[i]]) = ∑E[j]
- = ∑(Aj*E[1] + Bj*E[father[j]] + Cj)
- = ∑(Aj*E[1] + Bj*E[i] + Cj)
- 带入上面的式子得
- (1 - (1-ki-ei)/m*∑Bj)*E[i] = (ki+(1-ki-ei)/m*∑Aj)*E[1] + (1-ki-ei)/m*E[father[i]] + (1-ki-ei) + (1-ki-ei)/m*∑Cj;
- 由此可得
- Ai = (ki+(1-ki-ei)/m*∑Aj) / (1 - (1-ki-ei)/m*∑Bj);
- Bi = (1-ki-ei)/m / (1 - (1-ki-ei)/m*∑Bj);
- Ci = ( (1-ki-ei)+(1-ki-ei)/m*∑Cj ) / (1 - (1-ki-ei)/m*∑Bj);
- 对于叶子结点
- Ai = ki;
- Bi = 1 - ki - ei;
- Ci = 1 - ki - ei;
- 从叶子结点开始,直到算出 A1,B1,C1;
- E[1] = A1*E[1] + B1*0 + C1;
- 所以
- E[1] = C1 / (1 - A1);
- 若 A1趋近于1则无解...
- **/
注意:
1.若期望走的步数为无穷大,输出impossible
2.这道题卡精度,刚开始的时候精度设置为1e-8,wa了,改为1e-9才ac
我自己写的代码:
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 6 using namespace std; 7 8 const int maxn=1e4+10; 9 const double eps=1e-9; 10 11 double a[maxn]; 12 double b[maxn]; 13 double c[maxn]; 14 double k[maxn]; 15 double e[maxn]; 16 double res[maxn];//方便计算 17 18 struct Edge 19 { 20 int to,next; 21 }; 22 Edge edge[maxn<<1]; 23 int head[maxn],tot; 24 int sum[maxn]; //有多少条边和i相连 25 26 inline int sgn(double x) 27 { 28 if(fabs(x)<eps) 29 return 0; 30 return x>0?1:-1; 31 } 32 33 void init() 34 { 35 memset(head,-1,sizeof head); 36 tot=0; 37 memset(sum,0,sizeof sum); 38 } 39 40 void addedge(int u,int v) 41 { 42 edge[tot].to=v; 43 edge[tot].next=head[u]; 44 head[u]=tot++; 45 } 46 47 void cal_res(int N) 48 { 49 for(int i=1;i<=N;i++) 50 res[i]=(1.0-e[i]-k[i])/(double)sum[i]; 51 } 52 53 void dfs(int pre,int u) 54 { 55 if(u!=1&&sum[u]==1) 56 { 57 a[u]=k[u]; 58 b[u]=1.0-e[u]-k[u]; 59 c[u]=1.0-e[u]-k[u]; 60 return ; 61 } 62 double cnta=0.0; 63 double cntb=0.0; 64 double cntc=0.0; 65 for(int i=head[u];~i;i=edge[i].next) 66 { 67 int v=edge[i].to; 68 if(v==pre) 69 continue; 70 dfs(u,v); 71 cnta+=a[v]; 72 cntb+=b[v]; 73 cntc+=c[v]; 74 } 75 a[u]=(k[u]+res[u]*cnta)/(1.0-res[u]*cntb); 76 b[u]=(res[u])/(1.0-res[u]*cntb); 77 c[u]=(res[u]*cntc+1.0-e[u]-k[u])/(1.0-res[u]*cntb); 78 return ; 79 80 } 81 int main() 82 { 83 int test; 84 scanf("%d",&test); 85 int cas=1; 86 while(test--) 87 { 88 init(); 89 90 printf("Case %d: ",cas++); 91 int N; 92 scanf("%d",&N); 93 94 for(int i=1;i<N;i++) 95 { 96 int u,v; 97 scanf("%d%d",&u,&v); 98 addedge(u,v); 99 addedge(v,u); 100 sum[u]++; 101 sum[v]++; 102 } 103 for(int i=1;i<=N;i++) 104 { 105 scanf("%lf%lf",&k[i],&e[i]); 106 k[i]=k[i]/100.0; 107 e[i]=e[i]/100.0; 108 } 109 cal_res(N); 110 dfs(-1,1); 111 if(sgn(1.0-a[1]-0)<=0) 112 printf("impossible\n"); 113 else 114 { 115 double ans=c[1]/(1.0-a[1]); 116 printf("%.6f\n",ans); 117 } 118 } 119 return 0; 120 }