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 = 55;
 9 double dp[N][M];
10 int n, m, t, d;
11 vector<int> v[M]; //vector表示不定长数组
12
13 double solve(int x)
14 {
15    double ans = 0;
16    memset(dp, 0, sizeof(dp));
17    for(int i = 1; i <= n; i++) dp[0][i] = 1.0/n;// 表示还没走时,走到每个点的概率
18    for(int i = 0; i < d; i++)
19    {
20        for(int j = 1; j <= n; j++)
21        {
22            if(j == x) continue; //不会走到与自己相同的点上
23            int s = v[j].size(); //与点j相连的数有多少个
24            for(int k = 0; k < s; k++)
25            {
26                int c = v[j][k]; //与j相连的第k个数
27                dp[i+1][c] += dp[i][j]*1.0/s; //这里很难给你讲清楚,自己好好思考
28                                             //心中有个具体的场景就更好
29            }
30        }
31    }
32    for(int i = 1; i <= n; i++)
33    {
34        if(i == x) continue;
35        ans += dp[d][i]; //第d步到其它所有点的概率之和为
36                         //第d步没走到第x点的概率。
37    }
38    return ans;
39 }
40
41 int main()
42 {
43     int a, b;
44     scanf("%d", &t);
45     while(t--)
46     {
47         scanf("%d %d %d", &n, &m, &d);
48         for(int i = 1; i <= n; i++) v[i].clear();
49         while(m--)
50         {
51             scanf("%d %d", &a, &b);
52             v[a].push_back(b); //向尾部添加元素,关于vector的用法,自己参考资料
53             v[b].push_back(a);
54         }
55         for(int i = 1; i <= n; i++) printf("%.10lf\n", solve(i));
56     }
57     return 0;
58 }

时间: 2024-10-14 21:21:03

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 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(概率)

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 5001 Walk (暴力)

每次都去掉一个点求出到达 其他点的概率就是不能到达这个点的概率. Walk Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 51    Accepted Submission(s): 37 Special Judge Problem Description I used to think I could be anything, b

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

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