hdu4035----Maze

Maze

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

Total Submission(s): 1788    Accepted Submission(s): 701

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

概率dp,状态方程想到了,但是没有去化简,看了kuangbin的博客做的,自己在纸上也试着推了一遍,看来以后做概率dp的时候如果感觉想到方程但没什么思路就多化化简.

/*************************************************************************
    > File Name: hdu4035.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2014年12月21日 星期日 17时19分33秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 10010;
const double eps = 1e-9;
int  n;
double k[N], e[N];
double A[N], B[N], C[N];
vector <int> edge[N];

bool dfs(int u, int fa)
{
	int m = edge[u].size();
	A[u] = m *  k[u];
	B[u] = (1 - k[u] - e[u]);
	C[u] = m * (1 - k[u] - e[u]);
	double f = m;
	for (int i = 0; i < m; ++i)
	{
		int v = edge[u][i];
		if (v == fa)
		{
			continue;
		}
		if (!dfs(v, u))
		{
			return false;
		}
		A[u] += (1 - k[u] - e[u]) * A[v];
		C[u] += (1 - k[u] - e[u])  * C[v];
		f -= (1 - k[u] - e[u]) * B[v];
	}
	if (abs(f) <= eps)
	{
		return false;
	}
	A[u] /= f;
	B[u] /= f;
	C[u] /= f;
	return true;
}

int main()
{
	int t;
	int u, v;
	scanf("%d", &t);
	int icase = 1;
	while (t--)
	{
		memset (A, 0, sizeof(A));
		memset (B, 0, sizeof(B));
		memset (C, 0, sizeof(C));
		scanf("%d", &n);
		for (int i = 1; i <= n; ++i)
		{
			edge[i].clear();
		}
		for (int i = 1; i <= n - 1; ++i)
		{
			scanf("%d%d", &u, &v);
			edge[u].push_back(v);
			edge[v].push_back(u);
		}
		for (int i = 1; i <= n; ++i)
		{
			scanf("%lf%lf", &k[i], &e[i]);
			k[i] /= 100;
			e[i] /= 100;
		}
		printf("Case %d: ", icase++);
		if (!dfs(1, -1) || abs(A[1] - 1) <= eps)
		{
			printf("impossible\n");
		}
		else
		{
			printf("%.6f\n", C[1] / (1 - A[1]));
		}
	}
	return 0;
}
时间: 2024-11-08 18:19:01

hdu4035----Maze的相关文章

[hdu4035]Maze——树上高消

题目大意: 给定一颗树,从1号节点开始,在每个节点都有三种可能: 1.以\(k_i\)的概率回到1号节点 2.以\(e_i\)的概率走出迷宫 3.和该点相连的边随机走一条 求走出迷宫期望下走的步数. 思路: 首先设\(p_i=1-k_i-e_i\). 设从第\(i\)个点出发,期望意义下走出迷宫需要走的步数为\(f_i\),那么我们得到的递推式是: \[ f_i=k_i\times f_1+p_i\times (1+\frac{f_{fa}+\sum_vf_v}{d_i}) \] 不难发现这个递

[hdu4035] Maze【概率dp 数学期望】

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4035 真的是一道好题,题解比较麻烦,我自己在纸上写了好大一块草稿才搞出来,不用公式编辑器的话就很难看清楚,所以不上题解啦,贴一个题解的链接:http://blog.csdn.net/balloons2012/article/details/7891054 注意此题卡精度,我一开始eps是1e-8,WA掉了,开到了1e-10,AC~,真是烦卡精度的题. #include <cstdio> #inclu

HDU-4035 Maze (概率DP求期望)

题目大意:在一个树形迷宫中,以房间为节点.有n间房间,每间房间存在陷阱的概率为ki,存在出口的概率为ei,如果这两种情况都不存在(概率为pi),那么只能做出选择走向下一个房间(包括可能会走向上一个房间).根节点为1,当遇到陷阱时必须返回到根节点1处重新开始,当遇到出口时,走出迷宫.问从开始到走出迷宫所做出选择次数的期望值. 题目分析:定义状态dp(i)表示在节点 i 处直到走出迷宫的选择次数期望值.则状态转移方程为: dp(i)=ki*dp(1)+(1/m)*pi*∑(dp(son)+1) (i

HDU4035 Maze(期望DP)

题意 抄袭自https://www.cnblogs.com/Paul-Guderian/p/7624039.html 有n个房间,由n-1条隧道连通起来,形成一棵树,从结点1出发,开始走,在每个结点i都有3种可能(概率之和为1):1.被杀死,回到结点1处(概率为ki)2.找到出口,走出迷宫 (概率为ei)3.和该点相连有m条边,随机走一条求:走出迷宫所要走的边数的期望值.(2≤n≤10000) Sol 非常nice的一道题. 我简单的说一下思路:首先列出方程,$f[i]$表示在第$i$个位置走出

HDU4035 Maze 【树形DP】【期望DP】

题目分析: 以前一直不会这个方法, 我好菜啊. 转移分为三个部分,一个是直接成功,一个是转移到E1,还有一个是转移到自己周围的一圈儿点. 如果是叶子那么只能转移到父亲,如果不是叶子可以把非叶子的转移代换,这样也只转移到父亲,判一下无解就行了. 代码: 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 10200; 5 const double eps = 1e-10; 6 7 int n,Tmp,cas;

[题解]HDU4035 Maze

传 题目描述 一棵\(n\)个节点的树,从1号结点开始游戏,在每一个点\(x\): 有\(a[x]/100\)的可能掉进陷阱死翘翘回到1重新开始 有\(b[x]/100\)的可能找到出口并结束游戏 剩下的可能中,你等概率随机选一条和它相连的边(可以是父亲)走过去 问期望多少步结束游戏 \(1\leq T \leq 30,1\leq n \leq 10000\) 分析 设\(dp[x]\)表示位于\(x\)节点时,期望走几步才能结束游戏 那么\(dp[x]=0.01a[x]*dp[1]+0.01b

【HDU4035】Maze

1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <cmath> 7 8 using namespace std; 9 const int maxn=10000+100; 10 int T,n; 11 double E[maxn],K[maxn],A[m

hdu4035之经典慨率DP

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 1419    Accepted Submission(s): 511 Special Judge Problem Description When wake up, lxhgww find himself in a huge maze. The maze consisted b

hdu 5094 Maze bfs+状态压缩

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 642    Accepted Submission(s): 229 Problem Description This story happened on the background of Star Trek. Spock, the deputy captain of St

[LeetCode] The Maze III 迷宫之三

There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up (u), down (d), left (l) or right (r), but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction. T