hdoj 5001 Walk 【概率DP】 【在步数限制下 求不经过一个点的概率】

Walk

Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 728    Accepted Submission(s): 455
Special Judge

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
 
题意:给你一个N个点和M条双向边的图,每条边是一步的距离。现在要求任选起点出发 走k步(但不允许在未走够k步之前停止),中间可以经过一个点多次。 问你在满足要求 的前提下 不经过 u 点的概率(1 <= u  <= N)。


首先用dp[ i ][ j ]表示在第 j 步到达 i 点的概率。

对于一条边<a, b>。显然有 dp[ a ][ j ] += dp[ b ][ j-1] * (1 / size)。(size表示与a有直接边相连的点的数目)


思路:时间限制15000ms, 直接三重循环暴力。
对每个 点 u (1 <= u <= N),求出经过 u 的总概率 ans =  (求和)dp[ u ][ j ] (0 <= j <= N)。 用 1 减去ans 就ok了。


在求经过u点总概率时,要分两种情况讨论

一:起点在u点,概率显然为 1.0 / N;

二:起点不在u点,那么只能选择除u之外的N-1个点(但是任选一点的概率还是 1.0 / N)。
对于第二种情况注意在初始化dp数组时,有dp[ i ][ 0 ] = 1.0 / N (1 <= i <= N && i != u)。

AC代码:


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
double dp[60][10010];//dp[i][j]存储第j步到达i的概率
vector<int> G[60];
int N, M, step;
void init()
{
    for(int i = 1; i <= N; i++)
        G[i].clear();
}
void getMap()
{
    int x, y;
    while(M--)
    {
        scanf("%d%d", &x, &y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
}
double getans(int u)
{
    double ans = 0;//经过u的总概率
    memset(dp, 0, sizeof(dp));
    for(int i = 1; i <= N; i++)
    {
        if(i == u) continue;
        dp[i][0] = 1.0 / N;//第一次选择起点 概率
    }
    for(int j = 1; j <= step; j++)//最多走的步数
    {
        for(int i = 1; i <= N; i++)//遍历所有点
        {
            if(i == u) continue;//不能经过u
            double p = 1.0 / G[i].size();//可以选择任意一个与它有边相连的点
            for(int k = 0; k < G[i].size(); k++)
            {
                int v = G[i][k];
                dp[v][j] += dp[i][j-1] * p;
            }
        }
        ans += dp[u][j];//第j步到达u的概率
    }
    return 1 - ans - 1.0 / N;
}
void solve()
{
    for(int i = 1; i <= N; i++)
        printf("%.10lf\n", getans(i));
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d%d", &N, &M, &step);
        init();
        getMap();
        solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值