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 to an adjacent node with the same probability in the next step. I will pick up the start node randomly (each node in the graph has the same probability.), and travel for d steps, noting that I may go through some nodes multiple times.
If I miss some sights at a node, it will make me unhappy. So I wonder for each node, what is the probability that my path doesn‘t contain it.

Input

The first line contains an integer T, denoting the number of the test cases.
For each test case, the first line contains 3 integers n, m and d, denoting the number of vertices, the number of edges and the number of steps respectively. Then m lines follows, each containing two integers a and b, denoting there is an edge between node a and node b.
T<=20, n<=50, n-1<=m<=n*(n-1)/2, 1<=d<=10000. There is no self-loops or multiple edges in the graph, and the graph is connected. The nodes are indexed from 1.

Output

For each test cases, output n lines, the i-th line containing the desired probability for the i-th node.
Your answer will be accepted if its absolute error doesn‘t exceed 1e-5.

Sample Input


2
5 10 100
1 2
2 3
3 4
4 5
1 5
2 4
3 5
2 5
1 4
1 3
10 10 10
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
4 9

Sample Output


0.0000000000
0.0000000000
0.0000000000
0.0000000000
0.0000000000
0.6993317967
0.5864284952
0.4440860821
0.2275896991
0.4294074591
0.4851048742
0.4896018842
0.4525044250
0.3406567483
0.6421630037

 

题目大意:在一幅图中,主人公随机选择一个点为起始点,然后每一步都是随机选择相邻点进行访问,一共走了d步。求各个点没有被访问的概率。

概率dp入门题,定义dp[u][i][j]:经过了j步之后落在i点且从来不经过u点的概率。最后对于各个点,从不经过它的概率就是dp[u][*][d]的和。

在实际操作过程中,可以把dp数组变成二维的,即变成dp[i][j]。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
const int maxn = 55;
int head[maxn],cur;
struct Edge
{
    int to;
    int next;
} edge[maxn*2];
void init()
{
    cur=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v)
{
    edge[cur].to = v;
    edge[cur].next = head[u];
    head[u] = cur++;
}
double dp[maxn][10005];
double ans[maxn];
int du[maxn],n;
void cal_dp(int x,int d)
{
    for(int u=1; u<=n; u++)
    {
        dp[u][d] = 0;
        if(u==x) continue;
        for(int e=head[u]; e!=-1; e=edge[e].next)
        {
            int v = edge[e].to;
            if(v==x) continue;
            dp[u][d] += dp[v][d-1]/du[v];
        }
    }
}
int main()
{
    int T,u,v,m,d;
    scanf("%d",&T);
    while(T--)
    {
        init();
        memset(du,0,sizeof(du));
        scanf("%d%d%d",&n,&m,&d);
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
            du[u]++;
            du[v]++;
        }
        for(int i=1; i<=n; i++)
        {
            dp[i][0] = 1.0/n;
            ans[i]=0;
        }
        for(int u=1; u<=n; u++)
        {
            for(int j=1; j<=d; j++)
                cal_dp(u,j);
            for(int v=1;v<=n;v++)
                ans[u] += dp[v][d];
        }
        for(int i=1; i<=n; i++)
        {
            printf("%.10lf\n",ans[i]);
        }
    }
}
时间: 2024-10-15 01:33:20

hdu 5001 walk 概率dp入门题的相关文章

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

概率DP入门题

一 概率问题的论文 1.算法合集之<信息学竞赛中概率问题求解初探> 2.有关概率和期望问题的研究 3.算法合集之<浅析竞赛中一类数学期望问题的解决方法> 二 入门题目 1.POJ 3744 Scout YYF I (简单题) 题意:一条路上有n个地雷 ,a[i]代表第i个地雷放的位置,求安全走过这段路的概率 分析:若第k个位置有地雷则安全走过这个位置的方案为在第k-1个位置跳两步概率为(1-p) 从反面考虑 已经安全走过了第i-1个雷 则在第i个雷的死掉的概率为 1-p(从走到a[

HDU 4089 Activation (概率dp 好题 + 难题)

Activation Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1842    Accepted Submission(s): 689 Problem Description After 4 years' waiting, the game "Chinese Paladin 5" finally comes out.

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 4405 Aeroplane chess 概率dp入门题

Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal probability to face up and the numbers on the faces are 1,2,3

hdu 5001 Walk(概率)

http://acm.hdu.edu.cn/showproblem.php?pid=5001 应该算是一道简单的概率题.想了两个多小时,结果越想越麻烦.开了一个三维数组,MLE了.. 最后借鉴实验室学长的思路,发现这样想很直观,正退就可以. 设dp[j][d]表示不能经过i点走了d步到达j点的概率.那么dp[j][d] = ∑ dp[k][d-1]/edge[k].size().那么不经过i点的概率为∑dp[j][D]. #include <stdio.h> #include <iost

hdu 4405(概率dp简单题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1535    Accepted Submission(s): 1050 Problem Description Hzz loves aeroplane

HDU 4576 Robot 概率DP 水题

Robot Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total Submission(s): 3851    Accepted Submission(s): 1246 Problem Description Michael has a telecontrol robot. One day he put the robot on a loop with n cells.

HDU 3853-loop(概率dp入门)

题意: r*c个方格,从(1,1)开始在每个方格可释放魔法(消耗能量2)以知,释放魔法后可能在原地.可能到达相邻的下面格子或右面格子,给出三者的概率 求要到达(R,C)格子,要消耗能量的期望值. 分析: 状态好确定,dp[i][j]表示(i,j)到达(r,c)还需要的能量值,则dp[r][c]=0,dp[1][1]就是答案 dp[i][j]=dp[i][j]*p[i][j][0]+dp[i][j+1]*p[i][j][1]+dp[i+1][j]*p[i][j][2]+2.0,再移项即可; #in