HDU-4035-概率dp-期望-公式化简
Maze
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 3149 Accepted Submission(s): 1370
Special Judge
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?
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.
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
Case 2: impossible
Case 3: 2.895522
给出一颗树,起始点为1号点,接下来可以走向任意一个与当前点连接着的点,每个点有ki%的概率被杀死和ei%的概率逃生(ki+ei<=100),被杀死后将返回1号点从新开始,问逃生成功的期望步数,如果不可能逃生成功输出impossible。注意到在每个点只有三种情况死亡,逃生或者是等概率的走向其他点,走向每个点的概率就是(1-ki-ei)/ni,ni为连接点个数。设f[i]表示在i点距离逃生成功的期望步数,我们有:
f[i]=k[i]*f[1]+e[i]*0+( (1-k[i]-e[i])/ni )*( (f[fa[i]]+1) + SUM{ f[j]+1 } )
注意到相关联的系数只有f[1],f[fa[i]]和常数C,令f[i]=A[i]*f[1]+B[i]*f[fa[i]]+C[i],代入f[j]得到
(1-SUM{ B[j] }*(1-ki-ei)/ni)*f[i]=(ki+SUM{A[j]}*(1-ki-ei)/ni)*f[1]+(1-ki-ei)/ni*f[fa[i]]+(1-ki-ei)/ni*(ni+SUM{C[j]} )
令Pi=(1-ki-ei)/ni
得到A[i]=(ki+Pi*SUM{A[j]})/(1-Pi*SUM{B[j]})
B[i]=Pi/(1-Pi*SUM{B[j]})
C[i]=Pi*(ni+SUM{C[j]})/(1-Pi*SUM{B[j]})
然后树形dp一下由叶子向父亲不断递推得到A[1]和C[1],答案就是C[1]/(1-A[1]),如果不可能逃生成功的话,1-A[1]应该趋近于零,特判一下。
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<stack>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<time.h>
#include<algorithm>
using namespace std;
#define mp make_pair
#define pb push_back
#define debug puts("debug")
#define LL long long
#define pii pair<int,int>
#define eps 1e-10
#define inf 0x3f3f3f3f const int NN=;
double A[NN],B[NN],C[NN],f[];
double E[NN],K[NN];
vector<int>g[NN];
void dfs(int u,int fa){
int ni=g[u].size(); //临接点个数
double _A=,_B=,_C=;
for(int i=;i<g[u].size();++i){
int v=g[u][i];
if(v==fa) continue;
dfs(v,u);
_A+=A[v],_B+=B[v],_C+=C[v];
}
double P=(1.00-K[u]-E[u])/ni;
A[u]=(K[u]+P*_A)/(1.00-P*_B);
B[u]=P/(1.00-P*_B); if(u==) B[u]=;
C[u]=P*(ni+_C)/(1.00-P*_B);
}
int main()
{
int t,i,j,k,n,m,u,v;
scanf("%d",&t);
for(int cas=;cas<=t;++cas){
scanf("%d",&n);
for(i=;i<=n;++i) g[i].clear();
for(i=;i<n;++i){
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
for(i=;i<=n;++i){
scanf("%lf%lf",K+i,E+i);
K[i]/=;
E[i]/=;
}
dfs(,-);
double ans=C[]/(1.00-A[]);
if(fabs(1.00-A[])<eps) printf("Case %d: impossible\n",cas);
else printf("Case %d: %.6f\n",cas,ans);
}
return ;
}
HDU-4035-概率dp-期望-公式化简的更多相关文章
- 2017 ICPC Asia Urumqi A.coins (概率DP + 期望)
题目链接:Coins Description Alice and Bob are playing a simple game. They line up a row of nn identical c ...
- luogu P6835 概率DP 期望
luogu P6835 概率DP 期望 洛谷 P6835 原题链接 题意 n + 1个节点,第i个节点都有指向i + 1的一条单向路,现在给他们添加m条边,每条边都从一个节点指向小于等于自己的一个节点 ...
- hdu 3853 概率dp
题意:在一个R*C的迷宫里,一个人在最左上角,出口在右下角,在每个格子上,该人有几率向下,向右或者不动,求到出口的期望 现在对概率dp有了更清楚的认识了 设dp[i][j]表示(i,j)到(R,C)需 ...
- hdu 4035 可能性DP 成都网络游戏
http://acm.hdu.edu.cn/showproblem.php?pid=4035 获得: 1.首先推断是不是树.事实上,所有的感觉身影,既看边数==算-1是不成立 2.有时候,我告诉孩子来 ...
- 概率dp+期望dp 题目列表(一)
表示对概率和期望还不是很清楚定义. 目前暂时只知道概率正推,期望逆推,然后概率*某个数值=期望. 为什么期望是逆推的,例如你求到某一个点的概率我们可以求得,然后我们只要运用dp从1~n每次都加下去就好 ...
- HDU 4599 概率DP
先推出F(n)的公式: 设dp[i]为已经投出连续i个相同的点数平均还要都多少次才能到达目标状态. 则有递推式dp[i] = 1/6*(1+dp[i+1]) + 5/6*(1+dp[1]).考虑当前这 ...
- HDU 5001 概率DP || 记忆化搜索
2014 ACM/ICPC Asia Regional Anshan Online 给N个点,M条边组成的图,每一步能够从一个点走到相邻任一点,概率同样,问D步后没走到过每一个点的概率 概率DP 測 ...
- HDU 4815 概率dp,背包
Little Tiger vs. Deep Monkey Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K ( ...
- hdu 4050(概率dp)
算是挺简单的一道概率dp了,如果做了前面的聪聪于可可的话,这题不需要什么预处理,直接概率dp就行了... #include <stdio.h> #include <stdlib.h& ...
- hdu 3853 LOOPS(概率 dp 期望)
Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help ...
随机推荐
- MySQL逗号分割字段的列转行
前言: 由于很多业务表因为历史原因或者性能原因,都使用了违反第一范式的设计模式.即同一个列中存储了多个属性值(具体结构见下表). 这种模式下,应用常常需要将这个列依据分隔符进行分割,并得到列转行的结果 ...
- fastBoot使用
fastBoot使用 作者:李老师,华清远见嵌入式学院讲师. 1.1. 使用Fastboot方式烧写 在安卓手机中Fastboot是一种比Recovery更底层的刷机模式.Fastboot是一种线刷, ...
- How many ways??---hdu2157(矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2157 题意:有一个有向图,含有n个节点,m条边,Q个询问,每个询问有 s,t,p,求 s 到 t ...
- JDK安装(linux系统)
安装好centos6.8以后,输入命令: 如果显示如下: 证明系统自带了JDK,需要手动删除. 然后搜索: 备注: 查询一个包是否被安装 # rpm -q < rpm package name& ...
- mysql 约束条件 primary key 主键
primary key字段的值不为空且唯一 约束:not null unique 存储引擎:innodb 对于innodb来说,一张表内必须有一个主键 单列做主键多列做主键(复合主键) 通常都是id字 ...
- Python 之各种推导式玩法
推导式套路 之前我们已经学习了最简单的列表推导式和生成器表达式.但是除此之外,其实还有字典推导式.集合推导式等等. 下面是一个以列表推导式为例的推导式详细格式,同样适用于其他推导式. variable ...
- fold change的意义[转载]
转自:https://zhidao.baidu.com/question/2052933434631672387.html 1.解释 解释:表达值倍数变化 ,分析,消除可能的混杂因素,必要时可以用读段 ...
- 用ildasm/ilasm修改IL代码(操作步骤)
在开发中遇到这样一个场景,需要修改一个dll文件(.NET程序集)中某些地方的类型名称,但没有源代码,只能修改IL代码. 操作步骤如下: 1. 运行ildasm ildasm是由微软提供的.NET程序 ...
- VS2010/MFC编程入门之九(对话框:为控件添加消息处理函数)
创建对话框类和添加控件变量在上一讲中已经讲过,这一讲的主要内容是如何为控件添加消息处理函数. MFC为对话框和控件等定义了诸多消息,我们对它们操作时会触发消息,这些消息最终由消息处理函数处理.比如我们 ...
- Java面试:投行的15个多线程和并发面试题
多线程和并发问题已成为各种 Java 面试中必不可少的一部分.如果你准备参加投行的 Java 开发岗位面试,比如巴克莱银行(Barclays).花旗银行(Citibank).摩根史坦利投资公司(Mor ...