您的位置:首页 > 其它

【HDU4035】【Maze】【概率dp】【数列求和】

2015-08-17 17:19 274 查看


Maze

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)

Total Submission(s): 2058 Accepted Submission(s): 828

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



Recommend

lcy

dp[i] 表示已经i个相同/不同的情况下的期望值

dp[0] = 1 + dp[1]

dp[1] = 1 + (dp[2] + dp[1]*(m-1))/m

dp[2] = 1 + (dp[3] + dp[1]*(m-1))/m

dp[i] = 1 + (dp[i+1] + dp[1]*(m-1)) / m

dp
= 0

dp[i+1] = 1 + (dp[i+2] + dp[1]*(m-1))/m

dp[i] - dp[i+1] = (dp[i+1] - dp[i+2])/ m;

c[i] = dp[i] - dp[i+1]

c[i+1] = m * c[i]

c[0] = 1

c[i] = $m^{i}$

然后继续

当i = 0 时 $m^{i} = 1 == dp[i]-dp[i+1]$

所以 dp[i] - dp[i+1] = $m^{i}$

然后dp[0] - dp[1] = $m^{0}$

dp[1] - dp[2] = $m^{1}$

dp[i-1] - dp[i] = $m^{i-1}$

dp[0] - dp[i] = (1-$m^{i}$)/(1-m)

dp[0] - dp
= (1-$m^{n}$)/(1-m)

dp[0] = (1-$m^{n}$)/(1-m)



当是不同的时候

dp[0] = 1 + dp[1]

dp[1] = 1 + (dp[1] + dp[2]*(m-1))/m

dp[2] = 1 + (dp[1] + dp[2] + dp[3]*(m-2))/m

dp[3] = 1 + (dp[1] + dp[2] + dp[3] + dp[4]*(m-3))/m

dp[i] = 1 + (dp[1] + dp[2] + dp[3] +...+ dp[i] + dp[i+1]*(m-i))/m

dp[i+1] = 1 + (dp[1] + dp[2] + dp[3] +...+ dp[i+1] + dp[i+2]*(m-i-1))/m

dp[i] - dp[i+1] = (dp[i+1] - dp[i+2])*(m-i-1)/m

设c[i] = dp[i] - dp[i+1]

c[i] = c[i+1] * (m-i-1)/m

c[0] = 1

dp[0] - dp[1] = c[0]

dp[1] - dp[2] = c[1]

dp[n-1] - dp
= c[n-1]

dp[0] = c[0] + c[1] + ... +c[n-1]

#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
    
int T;
int t,m,n;
double c[1000010];

int qpow(int x,int y)
{
	int ans = 1;
	int tt = x;
	while(y > 0)
	{
		if(y & 1) ans *= tt;
		tt *= tt;
		y >>= 1;
	}
	return ans;
}

int main()
{
 
	while(scanf("%d",&T) != EOF) 
    {
		while(T--)
		{
			memset(c,0,sizeof(c));
			scanf("%d%d%d",&t,&m,&n);
			if(t == 0)
			{
				printf("%lf\n",(1-qpow(m,n))*1.0/((double)(1-m)));
			}
			else
			{
				double ans = 0;
				c[0] = 1;
				ans += c[0];
				for(int i=0;i<=n-2;i++)
				{
					c[i+1] = c[i] * ((double)m) / (m-i-1);
					ans += c[i+1];
				}
				printf("%.11lf\n",ans);
			} 
			

		}

    }	   
  
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: