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/ ...
随机推荐
- 支持scrollTo的RecycleView
RecycleView内部没有帮我们实现ScrollTo的方法,不过帮我们实现了ScrollBy,我们可以通过ScrollBy自定义一个支持scrollTo的RecycleView. public c ...
- [Android framework学习] ViewGroup的addView函数分析
博客首页:http://www.cnblogs.com/kezhuang/p/ Android中整个的View的组装是采用组合模式. ViewGroup就相当与树根,各种Layout就相当于枝干,各种 ...
- C++析构函数可虚性探究
C++虚析构函数 析构函数是用来释放对象所申请的资源的函数. 当类内没有自定义的析构函数时,系统会自动调用默认的析构函数. 那么析构函数能否为虚函数呢? 虚函数的意义在于实现“多态性”.即:不同的个体 ...
- 【案例分享】crontab执行脚本异常问题
很多时候我们会遇见这种情况,我们千辛万苦写了一个脚本,经过测试,一切正常,然后放到了crontab里面执行,结果,不管怎么配置,就是执行不正常. 结果发现环境问题,居然是这个异常的元凶. 我们先在我们 ...
- SQL Server 数据库部分常用语句小结(一)
1. 查询某存储过程的访问情况 SELECT TOP 1000 db_name(d.database_id) as DBName, s.name as 存储名字, s.type_desc as 存储类 ...
- ES6使用的一些方法
查找数组中符合条件的所有记录 var list=[ {id:1,name:"张三"}, {id:2,name:"李四"}, {id:3,name:"王 ...
- Lua中string.format占位符的使用
虽然lua中字符串拼接"string.format"相对于".."消耗较大,但有时为了代码的可读性,项目中还是经常用到"string.format&q ...
- Python面试笔记四
数据库 1.将name字段添加索引 create index index_emp_name on student(name); 2.查询女生中数学成绩最高的分数 select max(score) f ...
- 构建高性能服务 Java高性能缓冲设计 vs Disruptor vs LinkedBlockingQueue
一个仅仅部署在4台服务器上的服务,每秒向Database写入数据超过100万行数据,每分钟产生超过1G的数据.而每台服务器(8核12G)上CPU占用不到100%,load不超过5.这是怎么做到呢?下面 ...
- @FeignClient
@FeignClient("APP-PROVIDER")public interface MyFeignClient { @RequestMapping(value = " ...