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/ ...
随机推荐
- 南京邮电大学java第四次实验报告
实 验 报 告 ( 2017 / 2018学年 第2学期) 课程名称 JAVA语言程序设计 实验名称 Java集成开发环境的安装与使用. Java变量.表达式与控制结构 实验时间 2018 年 6 月 ...
- kafka环境搭建
kafka环境搭建 for mac 对应qq群号:616961231 在之前的文章中, 有学习能力和兴趣爱好的同学,自己动手维护测试环境,丰衣足食是最好的办法,今天我们来讲讲kafka在mac上的安装 ...
- Ubuntu上更改MySQL数据库数据存储目录
之前写过一篇博客"MySQL更改数据库数据存储目录",当时的测试环境是RHEL和CentOS,谁想最近在Ubuntu下面更改MySQL数据库数据存储目录时遇到了之前未遇到的问题,之 ...
- Github速度慢的解决方法
首先ping一下github.global.ssl.fastly.net 得到相应的ip,例如我现在ping的ip是151.101.41.194 151.101.41.194 github.globa ...
- Maven替换为国内仓库
<mirror> <id>nexus-aliyun</id> <mirrorOf>central</mirrorOf> <name&g ...
- 20181112-PostgreSQL数据库dmp文件导入(记录一次数据导入)
20181112-PostgreSQL数据库dmp文件导入 标注:dmp文件导入,场景:多个schema导入 1. 环境准备: postgres集群master节点上,postgres用户执行以下操作 ...
- lnmp环境里安装mssql及mssql的php扩展
小活中用到mssql,于是在自己lnmp环境中安装各mssql数据库 步骤如下: 源码编译安装 (1)下载freetds-stable-0.91源码:http://download.csdn.net/ ...
- IIS 反向代理到 Apache、Tomcat
将请求的网址重写重定向到其它网址.当80端口被占用无法同时使用两个Web服务的解决方案,使得IIS和Apache Tomcat 共存 环境 WindowServer 2008 IIS7 Apache ...
- 2017 百度杯丶春秋欢乐赛 writeup
1. 内涵图(Misc) 题目: 我不是一个简单的图片 我是一个有内涵的图片 解:保存到桌面,右键属性->详细信息,即可获得flag. 2. 小电影(Misc) 题目: 我说过 这次比赛是让大家 ...
- [经验总结] 从其它sheet页引用数据生成图表时没有图例的解决办法
1.先给出一个在有数据区域的sheet页中生成的图表,比较全,图表和图例全部都有,如下图: 2.但是如果在其它 sheet页中引用该有数据的sheet数据时并生成图表,生成的图表只有图表区域显示,图例 ...