概率dp——期望水题hdu4405

还是逆推,如果遇到跳板直接继承目标地的期望即可

#include<bits/stdc++.h>
using namespace std;
#define maxn 200005
double dp[maxn];
int n,m,nxt[maxn];
int main(){
    while(scanf("%d%d",&n,&m)&&n){
        memset(nxt,0,sizeof nxt);
        memset(dp,0,sizeof dp);
        
        for(int i=1;i<=m;i++){
            int u,v;
            cin>>u>>v;
            nxt[u]=v;
        }
        
        for(int i=n-1;i>=0;i--)
            if(nxt[i]!=0){
                int tmp=i;
                while(nxt[tmp]!=0)
                    tmp=nxt[tmp];
                dp[i]=dp[tmp];
            }
            else {
                for(int j=1;j<=6;j++)
                    dp[i]+=(double)1/6 * (dp[i+j]+1);
            }
        printf("%.4lf\n",dp[0]);
    }
}

猜你喜欢

转载自www.cnblogs.com/zsben991126/p/11024085.html