首页 > 代码库 > hdu 5001 Walk(概率DP)

hdu 5001 Walk(概率DP)

水水的

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <vector>using namespace std;int t,n,m,d;vector<int>g[55];bool w[55][55];double dp[2][55];double dfs(int x){    double pp=1.0/n;    int now=0;    for(int i=1;i<=n;i++){        dp[now][i]=i==x?0:1;    }    for(int tt=1;tt<=d;tt++){        now^=1;        memset(dp[now],0,sizeof dp[now]);        for(int i=1;i<=n;i++){            if(i==x)continue;            int k=g[i].size();            double pp=1.0/k;            for(int j=0;j<g[i].size();j++){                int to=g[i][j];                if(to==x)continue;                dp[now][to]+=pp*dp[now^1][i];            }        }    }    double ans=0;    for(int i=1;i<=n;i++)ans+=dp[now][i];    return ans/n;}void solve(){    scanf("%d%d%d",&n,&m,&d);    for(int i=1;i<=n;i++)g[i].clear();    memset(w,0,sizeof w);    while(m--){        int u,v;        scanf("%d%d",&u,&v);        w[u][v]=w[v][u]=1;        g[u].push_back(v);        g[v].push_back(u);    }    for(int i=1;i<=n;i++){        printf("%.10f\n",dfs(i));    }}int main(){//    freopen("in","r",stdin);    scanf("%d",&t);    while(t--)solve();    return 0;}

 

hdu 5001 Walk(概率DP)