Maze HDU - 4035(期望dp)
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?
InputFirst 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.
OutputFor 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 类似的一题:hdu3853.
这题中给出的边是无向的,所以状态可以转移到1, fa[i], son[i], 三个地方。
令 dp[i] 表示从 i 位置走出迷宫的期望。
那么对于叶子结点:
dp[i] = k[i] * dp[1] + (1 - k[i] - e[i]) * (dp[fa[i]] + 1)
对于非叶子结点: len 表示 和结点 i 有关的边数, j 表示 i 的儿子节点
dp[i] = k[i] * dp[1] + (1 - k[i] - e[i]) / len * (dp[fa[i]] + 1 + Σ(dp[j] + 1))
令
dp[i] = A[i] * dp[1] + B[i] * dp[fa[i]] + C[i]
Σdp[j] = Σ(A[j] * dp[1] + B[j] * dp[i] + C[j])
代入非叶子结点的 dp[i] 中
dp[i] = k[i] * dp[1] + (1 - k[i] - e[i]) / len * (dp[fa[i]] + Σ(A[j] * dp[1] + B[j] * dp[i] + C[j])) + (1 - k[i] - e[i])
= (k[i] + (1 - k[i] - e[i]) / len * ΣA[j] * dp[1]
+ (1 - k[i] - e[i]) / len * dp[fa[i]]
+ (1 - k[i] - e[i]) / len * ΣB[j] * dp[i]
+ (1 - k[i] - e[i]) / len * ΣC[j] + (1 - k[i] - e[i])
移项,合并同类项得
(1 - (1 - k[i] - e[i]) / len * ΣB[j])dp[i] = (k[i] + (1 - k[i] - e[i]) / len * ΣA[j] * dp[1]
+ (1 - k[i] - e[i]) / len * dp[fa[i]]
+ (1 - k[i] - e[i]) *(ΣC[j] / len + 1)
然后通过这个式子推出A[1], B[1], C[1]
要求的是 dp[1], 代入一开始设的式子
dp[1] = A[1] * dp[1] + C[1]
dp[1] = C[1] / (1 - A[1])
当A[1] 和 1 很接近时,表示无解。
/*
.
';;;;;.
'!;;;;;;!;`
'!;|&#@|;;;;!:
`;;!&####@|;;;;!:
.;;;!&@$$%|!;;;;;;!'.`:::::'.
'!;;;;;;;;!$@###&|;;|%!;!$|;;;;|&&;.
:!;;;;!$@&%|;;;;;;;;;|!::!!:::;!$%;!$%` '!%&#########@$!:.
;!;;!!;;;;;|$$&@##$;;;::'''''::;;;;|&|%@$|;;;;;;;;;;;;;;;;!$;
;|;;;;;;;;;;;;;;;;;;!%@#####&!:::;!;;;;;;;;;;!&####@%!;;;;$%`
`!!;;;;;;;;;;!|%%|!!;::;;|@##%|$|;;;;;;;;;;;;!|%$#####%;;;%&;
:@###&!:;;!!||%%%%%|!;;;;;||;;;;||!$&&@@%;;;;;;;|$$##$;;;%@|
;|::;;;;;;;;;;;;|&&$|;;!$@&$!;;;;!;;;;;;;;;;;;;;;;!%|;;;%@%.
`!!;;;;;;;!!!!;;;;;$@@@&&&&&@$!;!%|;;;;!||!;;;;;!|%%%!;;%@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;;:''::;;%$!;;;;;;;|&@%;!$; `%&%!!$&:
'$$|;!!!!;;||;;;;;;;;;;%%;;;;;;;|@@|!$##; !$!;:!$&:
|#&|;;;;;;!||;;;;;;;;!%|;;;;!$##$;;;;|%' `%$|%%;|&$'
|&%!;;;;;;|%;;;;;;;;$$;;;;;;|&&|!|%&&; .:%&$!;;;:!$@!
`%#&%!!;;;;||;;;;;!$&|;;;!%%%@&!;;;!!;;;|%!;;%@$!%@!
!&!;;;;;;;;;||;;%&!;;;;;;;;;%@&!;;!&$;;;|&%;;;%@%`
'%|;;;;;;;;!!|$|%&%;;;;;;;;;;|&#&|!!||!!|%$@@|'
.!%%&%'`|$; :|$#%|@#&;%#%.
*/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pii pair<int, int>
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout) typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e4 + ;
const int maxm = 1e5 + ;
const int mod = 1e9 + ;
const ll INF = 1e18 + ;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-;
using namespace std; int n, m;
int cas, tol, T; std::vector<int> vec[maxn];
double A[maxn];
double B[maxn];
double C[maxn];
double k[maxn];
double e[maxn]; void init() {
for(int i=; i<=n; i++)
vec[i].clear();
mes(A, );
mes(B, );
mes(C, );
mes(k, );
mes(e, );
} void dfs(int u, int f) {
int len = vec[u].size();
if(len == && u != ) {
A[u] = k[u];
B[u] = C[u] = - k[u] - e[u];
return ;
}
if(A[u] != 0.0)
return ;
for(int i=; i<len; i++) {
int v = vec[u][i];
if(v == f) continue;
dfs(v, u);
}
double tmpa = 0.0, tmpb = 0.0, tmpc = 0.0;
for(int i=; i<len; i++) {
int v = vec[u][i];
if(v == f) continue;
tmpa += A[v];
tmpb += B[v];
tmpc += C[v];
}
double tmp = (1.0 - (1.0 - k[u] - e[u]) / len * tmpb);
A[u] = (k[u] + (1.0 - k[u] - e[u]) / len * tmpa) / tmp;
B[u] = (1.0 - k[u] - e[u]) / len / tmp;
C[u] = (1.0 - k[u] - e[u]) * (tmpc / len + ) / tmp;
} int main() {
int cas = ;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
init();
for(int i=; i<n; i++) {
int u, v;
scanf("%d%d", &u, &v);
vec[u].push_back(v);
vec[v].push_back(u);
}
for(int i=; i<=n; i++) {
scanf("%lf%lf", &k[i], &e[i]);
k[i] /= 100.0;
e[i] /= 100.0;
}
dfs(, -);
printf("Case %d: ", cas++);
if(fabs( - A[]) <= eps) {
printf("impossible\n");
} else {
double ans = C[] / ( - A[]);
printf("%.6f\n", ans);
}
}
return ;
}
Maze HDU - 4035(期望dp)的更多相关文章
- poj 2096 , zoj 3329 , hdu 4035 —— 期望DP
题目:http://poj.org/problem?id=2096 题目好长...意思就是每次出现 x 和 y,问期望几次 x 集齐 n 种,y 集齐 s 种: 所以设 f[i][j] 表示已经有几种 ...
- HDU 4035 期望dp
这道题站在每个位置上都会有三种状态 死亡回到起点:k[i] 找到出口结束 e[i] 原地不动 p[i] k[i]+e[i]+p[i] =1; 因为只给了n-1条路把所有都连接在一起,那么我们可以自然的 ...
- HDU 4405 期望DP
期望DP算是第一题吧...虽然巨水但把思路理理清楚总是好的.. 题意:在一个1×n的格子上掷色子,从0点出发,掷了多少前进几步,同时有些格点直接相连,即若a,b相连,当落到a点时直接飞向b点.求走到n ...
- hdu 4035 可能性DP 成都网络游戏
http://acm.hdu.edu.cn/showproblem.php?pid=4035 获得: 1.首先推断是不是树.事实上,所有的感觉身影,既看边数==算-1是不成立 2.有时候,我告诉孩子来 ...
- HDU 3853(期望DP)
题意: 在一个r*c的网格中行走,在每个点分别有概率向右.向下或停止不动.每一步需要的时间为2,问从左上角走到右下角的期望时间. SOL: 非常水一个DP...(先贴个代码挖个坑 code: /*== ...
- poj 2096 Collecting Bugs && ZOJ 3329 One Person Game && hdu 4035 Maze——期望DP
poj 2096 题目:http://poj.org/problem?id=2096 f[ i ][ j ] 表示收集了 i 个 n 的那个. j 个 s 的那个的期望步数. #include< ...
- hdu 4035 Maze(期待更多经典的树DP)
Maze Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Submi ...
- hdu 4035 2011成都赛区网络赛E 概率dp ****
太吊了,反正我不会 /* HDU 4035 dp求期望的题. 题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结点i都有3种可能: 1.被杀死,回到结点 ...
- HDU 4405 Aeroplane chess 期望dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Time Limit: 2000/1000 MS (Java/ ...
随机推荐
- 超级强大的socket工具ss,替代netstat
1.结论:ss 命令比netstat 更强大,提供功能更多,并且性能更高. 2.显示当前系统的socket占用总体宏观情况. ss -s 当已创建的socket数过多时,已经说明系统配置存在问题. 3 ...
- android找不到aar包
转载请标明出处,维权必究:https://www.cnblogs.com/tangZH/p/9939663.html 在做项目的时候引入aar包,编译的时候却提示错误(这个错误大概说的是...... ...
- huffman树即Huffma编码的实现
自己写的Huffman树生成与Huffman编码实现 (实现了核心功能 ,打出了每个字符的huffman编码 其他的懒得实现了,有兴趣的朋友可以自己在我的基础增加功能 ) /* 原创文章 转载请附上原 ...
- ASP.NET Zero--Migration控制台应用程序
Migration控制台应用程序 AspNet Zero包含一个工具Migrator.exe,用于轻松迁移数据库.您可以运行此应用程序来创建/迁移host和租户数据库. 该应用程序从它自己的appse ...
- Unity2018 Open C# Project Error
错误日志 升级到Unity2018之后,使用 Open C# Project 打开VS工程,出现报错,无法启动VS. 错误日志如下: ArgumentException: Value does not ...
- 【Eclipse】springMVC介绍与配置
SpringMCV介绍: Spring MVC是一种基于Java的实现了MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动的,也就是使用 ...
- qt 打包发布 获取dll
发布前,获取所有qt dll包命令 生成的程序运行正常之后,找到项目的生成目录,比如 项目源码路径: C:\QtPros\hellomw\它的项目生成目录是C:\QtPros\build-hellom ...
- Windows 版 SourceTree 免登录跳过初始设置的方法
首先去官网下载最新的sourcetree安装包,点击打开下载地址. 下载完成后安装,等到他自启动开始提示你登录的时候,打开“我的电脑(此电脑)”,在最上边的输入栏输入%LocalAppData%\At ...
- AQS框架源码分析-AbstractQueuedSynchronizer
前言:AQS框架在J.U.C中的地位不言而喻,可以说没有AQS就没有J.U.C包,可见其重要性,因此有必要对其原理进行详细深入的理解. 1.AQS是什么 在深入AQS之前,首先我们要搞清楚什么是AQS ...
- web框架开发-模板层
你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python代码之中. def current_datetime(request): now = datet ...