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为叶子节点)  <1>

dp(i)=ki*dp(1)+(1/m)*pi*(dp(father)+1)+(1/m)*pi*∑(dp(son)+1)  (i为非叶子节点)  <2>

将<2>整理一下,得到:

dp(i)=ki*dp(1)+(1/m)*pi*dp(father)+(1/m)*pi*∑dp(son)+pi

显然,dp(i)都与dp(1)有关,另dp(i)=A(i)*dp(1)+B(i)*dp(father)+C(i)  <3>

将<3>带入∑dp(son),得到:

(1-(pi/m)*∑A(son))dp(i)=(ki+(pi/m)∑A(son))*dp(1)+(pi/m)*dp(father)+(pi/m)*∑C(son)+pi

显然,A(i),B(i),C(i)可得。

dp(1)=C(1)/(1-A(1)) 即为答案。

代码如下:

# include<iostream>
# include<cstdio>
# include<cmath>
# include<vector>
# include<cstring>
# include<algorithm>
using namespace std;

const int N=10005;
const int INF=100000;
const double eps=1e-9;

int n;
double k[N];
double e[N];
double A[N],B[N],C[N];
vector<int>G[N];

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

int main()
{
    int T,x,y,cas=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
            G[i].clear();
        for(int i=1;i<n;++i){
            scanf("%d%d",&x,&y);
            G[x].push_back(y);
            G[y].push_back(x);
        }
        for(int i=1;i<=n;++i){
            scanf("%lf%lf",k+i,e+i);
            k[i]/=100;
            e[i]/=100;
        }
        printf("Case %d: ",++cas);
        if(dfs(1,-1)&&fabs(1-A[1])>eps)
            printf("%.6lf\n",C[1]/(1-A[1]));
        else printf("impossible\n");
    }
    return 0;
}

  

时间: 2024-10-27 19:11:49

HDU-4035 Maze (概率DP求期望)的相关文章

hdu 4035 Maze (概率DP)

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

HDU 4035 Maze 概率DP 好题

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

HDU 4035:Maze 概率DP求期望(有环)

Maze 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4035 题意: 有N(2 ≤ N ≤ 10000)个房间和一堆双向边(不存在环),每个房间有ki和ei两个值,分别代表回到房间1和游戏结束的概率,求游戏结束时通过的边数的期望 题解: 一道很好很经典的求期望的题 设E[i]为以i为起点,直到游戏结束所通过边数的期望,则E[1]即所求答案 设fa代表父亲节点(由于不存在环,则图为一棵树,设1为根节点),∑ch代表所有孩子节点,size代表与这

HDU 4035 Maze 概率dp 难度:2

http://acm.hdu.edu.cn/showproblem.php?pid=4035 求步数期望,设E[i]为在编号为i的节点时还需要走的步数,father为dfs树中该节点的父节点,son为dfs树种该节点的子节点的集合,kl[i]为被杀掉的概率,ex[i]为逃出的概率 mv[i]=(1-kl[i]-ex[i])/(1+len(son)) 则明显 E[i]=(E[father]+1)*mv[i]+sigma((E[son]+1)*mv[i])+E[1]*K[i] 未知量是E[i],E[

HDU 4035 Maze 概率dp+树形dp

题解:点击打开链接 #include <cstdio> #include <iostream> #include <cstring> #include <queue> #include <algorithm> #include <map> #include <cmath> using namespace std; const double eps = 1e-9; const int N = 10010; vector<

HDU 4405 Aeroplane chess (概率DP求期望)

题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点需要步数的期望 其中有m个跳跃a,b表示走到a点可以直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点可以到走到i+1,i+2,i+3,i+4,i+5,i+6 点且每个点的概率都为1/6 所以dp[i]=(dp[i+1]+dp[i+2]+dp[i+3]+dp[i+4]+dp[i+5]+dp[i+6])/6  + 1(步数加一). 而对于有跳跃的点直接为dp[a]=dp[b]; #include<stdio.h>

HDU 4050 wolf5x (概率DP 求期望)

题意:有N个格子,1~N,起点在0,每个格子有一个状态(0,1,2,3),每次可以跨[a,b]步, 问走完N个格子需要步数的期望,每次尽量走小的步数,即尽量走a步,不能则走a+1,-- 状态0意味着你不能踏进对应的网格. 状态1意味着你可以??步入网格用你的左腿. 状态2意味着你可以??步入网格用你的右腿. 状态3意味着你可以进入网格用任何你的腿,而接下来的步骤中,您可以使用任何的腿;即你不需要遵循上述规则. 思路:借鉴了各路大神的思想理解了下. dp[i][j] :表示走到第 i 个格子在 j

HDU4336-Card Collector(概率DP求期望)

Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2195    Accepted Submission(s): 1034 Special Judge Problem Description In your childhood, do you crazy for collecting the beautifu

HDU3853-LOOPS(概率DP求期望)

LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 1864    Accepted Submission(s): 732 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help h

HDU4405-Aeroplane chess(概率DP求期望)

Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1182    Accepted Submission(s): 802 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids lab