HDU 5001 Walk 概率DP入门!

题目描述:

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 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

Source
2014 ACM/ICPC Asia Regional Anshan Online

题目分析:

在一个n个节点的无向图,有m条边,你有d次走这个图,每次走的方向都是随机的,你的初始位置也是随机的。问,走不到这每一个点的概率分别是多少?
概率dp的入门题。
代码有详细解释。

代码如下:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 55;
const int MAXD = 10050;

vector <int>edge[MAXN];
double dp[MAXD][MAXN];//dp[i][j]表示第i步走到j点的概率
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m,d;
        scanf("%d%d%d",&n,&m,&d);
        for(int i=0; i<MAXN; i++) edge[i].clear();
        for(int i=1; i<=m; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            edge[u].push_back(v);
            edge[v].push_back(u);
        }
        for(int k=1; k<=n; k++)
        {
            memset(dp,0,sizeof(dp));
            for(int i=1; i<=n; i++) dp[0][i]=1.0/n;//有1/n概率i为初始的位置
            for(int i=0; i<d; i++)
            {
                for(int j=1; j<=n; j++)
                {
                    if (k==j) continue;
                    for(int l=0; l<edge[j].size(); l++)
                    {
                        dp[i+1][edge[j][l]]+=dp[i][j]*1.0/edge[j].size();
                        //从j点有1/edge[j].size()的概率走到其连着的任意点
                    }   
                }
            }
            double ans=0.0;
            for(int i=1; i<=n; i++)
            {
                if (k!=i) ans+=dp[d][i];//把所有达到别的点的概率相加就是走不到k点概率
            }
            printf("%.10lf\n",ans);
        }
    }
    return 0;
}

你可能感兴趣的:(概率DP)