码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5001 Walk (暴力、概率dp)

时间:2014-09-15 09:51:58      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   java   ar   

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 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
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  5017 5016 5015 5014 5013 
 
 
设dp[j][d]表示不能经过i点走了d步到达j点的概率。那么dp[j][d] = ∑ dp[k][d-1]/edge[k].size()。那么不经过i点的概率为∑dp[j][D]。(转自:http://blog.csdn.net/u013081425/article/details/39254337) (我的代码是dp[d][j])
每次都去掉一个点求出到达 其他点的概率就是不能到达这个点的概率。(转自:http://blog.csdn.net/xu12110501127/article/details/39254403)

 

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<cstdio>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<map>
 9 #include<string>
10 
11 #define N 55
12 #define M 15
13 #define mod 1000000007
14 #define p 10000007
15 #define mod2 100000000
16 #define ll long long
17 #define LL long long
18 #define maxi(a,b) (a)>(b)? (a) : (b)
19 #define mini(a,b) (a)<(b)? (a) : (b)
20 
21 using namespace std;
22 
23 int T;
24 int n,m,d;
25 vector<int>bian[N];
26 int cnt[N];
27 double dp[10005][N];
28 double re;
29 
30 void ini()
31 {
32     int i;
33     int x,y;
34     memset(cnt,0,sizeof(cnt));
35     scanf("%d%d%d",&n,&m,&d);
36     for(i=1;i<=n;i++){
37         bian[i].clear();
38     }
39     while(m--){
40         scanf("%d%d",&x,&y);
41         bian[x].push_back(y);
42         bian[y].push_back(x);
43     }
44     for(i=1;i<=n;i++){
45         cnt[i]=bian[i].size();
46     }
47 }
48 
49 void solve()
50 {
51     int q,o,i;
52     for(q=1;q<=n;q++)
53     {
54         re=0;
55         memset(dp,0,sizeof(dp));
56         for(i=1;i<=n;i++){
57             if(i==q) continue;
58             dp[0][i]=1.0/n;
59         }
60 
61         for(o=1;o<=d;o++){
62             for(i=1;i<=n;i++){
63                 if(i==q) continue;
64                 for(vector<int>::iterator it=bian[i].begin();it!=bian[i].end();it++){
65                     dp[o][i]+=dp[o-1][*it]/cnt[*it];
66                 }
67             }
68         }
69 
70         for(i=1;i<=n;i++){
71             if(i==p) continue;
72             re+=dp[d][i];
73         }
74         printf("%.10f\n",re);
75     }
76 }
77 
78 void out()
79 {
80     //printf("%I64d\n",ans);
81 }
82 
83 int main()
84 {
85     //freopen("data.in","r",stdin);
86     scanf("%d",&T);
87     //for(int cnt=1;cnt<=T;cnt++)
88     while(T--)
89     //while(scanf("%I64d%I64d",&n,&m)!=EOF)
90     {
91         ini();
92         solve();
93       //  out();
94     }
95 
96     return 0;
97 }

 

HDU 5001 Walk (暴力、概率dp)

标签:des   style   blog   http   color   io   os   java   ar   

原文地址:http://www.cnblogs.com/njczy2010/p/3972154.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!