[概率dp] hdu 5001 Walk

题意:n个点(1~n),m条边,走k次,双向边,每次随机走一条路,起点也随机,问不走到各个点的概率是多少。

思路:

概率dp[i][j][k] 前i步 走到j 走不到k的概率。

那么状态转移就是 j能走到的点,传递所有dp[i][j][k]的值乘上概率。

代码:

#include"cstdlib"
#include"cstdio"
#include"cstring"
#include"cmath"
#include"stack"
#include"algorithm"
#include"iostream"
#include"vector"
using namespace std;
#define N 300000
double dp[3][55][55];  //因为内存问题 用滚动数组
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m,d;
        scanf("%d%d%d",&n,&m,&d);
        vector<int>edge[55];
        while(m--)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            edge[a].push_back(b);
            edge[b].push_back(a);
        }
        memset(dp,0,sizeof(dp));
        int i,j,k,l;
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=n; j++)
            {
                if(j!=i) dp[0][i][j]+=1.0/n;  //初始化,就是因为起点在i所以走不到j的概率都是n分之1
            }
        }
        for(i=1; i<=d; i++)
        {
            memset(dp[i%2],0,sizeof(dp[i%2]));
            for(j=1; j<=n; j++)
            {
                for(k=0; k<(int)edge[j].size(); k++)
                {
                    for(l=1; l<=n; l++)
                    {
                        if(j!=l&&j!=edge[j][k]) dp[i%2][edge[j][k]][l]+=dp[1-i%2][j][l]*1.0/edge[j].size();
                    }
                }
            }
        }
        double ans[55];
        memset(ans,0,sizeof(ans));
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=n; j++)
                if(i!=j) ans[i]+=dp[d%2][j][i];
        }
        for(i=1; i<=n; i++)
            printf("%.10f\n",ans[i]);
    }
    return 0;
}
时间: 2024-10-22 03:57:26

[概率dp] hdu 5001 Walk的相关文章

HDU 5001 Walk(鞍山网络赛E题)

HDU 5001 Walk 题目链接 思路:枚举每个要经过的点,然后进行状态转移,状态为dp[i][j],状态表示当前在j的点,已经走了i步,每次转移的时候,不从这个枚举的点出发,这样就可以求出所有路径经过该点的概率p, 然后1 - p就是不经过的答案 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; con

HDU - 5001 Walk(概率dp+记忆化搜索)

Walk I used to think I could be anything, but now I know that I couldn't do anything. So I started traveling. The nation looks like a connected bidirectional graph, and I am randomly walking on it. It means when I am at node i, I will travel to an ad

hdu 5001 walk 概率dp入门题

Description I used to think I could be anything, but now I know that I couldn't do anything. So I started traveling. The nation looks like a connected bidirectional graph, and I am randomly walking on it. It means when I am at node i, I will travel t

HDU 5001 Walk 求从任意点出发任意走不经过某个点的概率 概率dp 2014 ACM/ICPC Asia Regional Anshan Online

题意: 给定n个点m条边的无向图 问: 从任意点出发任意走d步,从不经过某个点的概率 dp[i][j]表示从不经过i点的前提下,走了d步到达j点的概率. #include <iostream> #include <cstdio> #include <string.h> #include <queue> #include <vector> #include <algorithm> #include <set> using n

HDU 5001 Walk

解题思路:这是一道简单的概率dp,只要处理好相关的细节就可以了. dp[d][i]表示走d步时走到i的改概率,具体参考代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<vector> 5 #include<cstring> 6 using namespace std; 7 const int N = 10005; 8 const int M =

[AC自动机+概率dp] hdu 3689 Infinite monkey theorem

题意: 给n个字母,和m次数. 然后输入n个字母出现的概率 然后再给一个目标串str 然后问m次中敲出目标串的概率是多少. 思路: AC自动机+概率dp的简单题. 首先建立trie图,然后就是状态转移了 dp版本: dp三重循环变量次数,节点数,和字母数 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #include"

Hdu 5001 Walk 概率dp

Walk Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5001 Description I used to think I could be anything, but now I know that I couldn't do anything. So I started traveling. The nation looks like a connected bid

HDU 5001 Walk (暴力、概率dp)

Walk Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 266    Accepted Submission(s): 183 Special Judge Problem Description I used to think I could be anything, but now I know that I couldn't d

[ACM] hdu 5001 Walk (概率DP)

Walk Problem Description I used to think I could be anything, but now I know that I couldn't do anything. So I started traveling. The nation looks like a connected bidirectional graph, and I am randomly walking on it. It means when I am at node i, I