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代表与这个房间直接相连的房间数,设X=1-ki-ei

可以推导出 E[i]=ki*E[1]+X*(1/size*(E[fa]+1/size*∑E[ch])+1)---公式1

设 E[i]=a[i]*E[1]+b[i]*E[fa]+c[i]

则 ∑E[ch]=∑(a[ch]*E[1]+b[ch]*E[i]+c[ch])

代入公式1可得 E[i]=( (ki+X/size*∑a[ch])*E[1]+X/size*∑E[fa]+X+X/size*∑(c[ch]) ) /(1-X/size*b[ch])

对比可得

  a[i]=(ki+X/size*∑a[ch])/(1-X/size*b[ch])

  b[i]=X/size/(1-X/size*b[ch])

  c[i]=(X+X/size*c[ch])/(1-X/size*b[ch])

E[1]=a[1]*E[1]+b[1]*E[fa]+c[1]=(a[1]*E[fa]+c[1])/(1-a[1])=c[1]/(1-a[1])

             

代码

#include<stdio.h>
#include<vector>
#include<math.h>
using namespace std;
const int N=10005;
const double eps=1e-9;
vector<int>q[N];
bool mark[N];
int fa[N],out[N],que[N],head,tail;
double k[N],e[N],A,B,C;
void dfs(int x=1)
{
  bool leaf=true;
  double p=1-k[x]-e[x];
  mark[x]=true;
  int m=q[x].size();
  double a=0.0,b=0.0,c=0.0;
  for(int i=0;i<q[x].size();++i)
  {
    fa[q[x][i]]=x;
    if(!mark[q[x][i]])
    {
      leaf=false;
      dfs(q[x][i]);
      a+=A,b+=B,c+=C;
    }
  }
  if(leaf)
  {
    A=k[x];
    B=C=p;
  }
  else
  {
    A=(k[x]+p/m*a)/(1-p/m*b);
    B=p/m/(1-p/m*b);
    C=(p+p/m*c)/(1-p/m*b);
  }
}
int main()
{
  int T,n,x,y,leaf,w=0;
  scanf("%d",&T);
  while(T--)
  {
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
    out[i]=0,mark[i]=false,q[i].clear();
    for(int i=1;i<n;++i)
    {
      scanf("%d%d",&x,&y);
      q[x].push_back(y);
      q[y].push_back(x);
    }
    for(int i=1;i<=n;++i)
    {
      scanf("%lf%lf",&k[i],&e[i]);
      k[i]/=100.0,e[i]/=100.0;
    }
    dfs();
    printf("Case %d: ",++w);
    if(fabs(1-A)<eps)printf("impossible\n");
    else printf("%.6f\n",C/(1-A));
  }
}

  

时间: 2024-10-03 02:02:07

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 难度: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