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

HDU-5001 Walk (概率DP)

时间:2018-09-15 20:56:02      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:number   概率   href   eof   content   ati   direction   efi   started   

 

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
题解:给你N个点,M条边(无向),然后可以走d步,让你求恰好不走i点(i 1~N)的概率 ;
概率DP;dp[i][j]:表示走i步,到达j点的概率;对于当前点,它一定是由其相邻点走过来的,则这个点的概率为这些和他相邻的点的概率和 : dp[j][k]+=dp[j-1][vec[k][l]]*1.0/vec[k].size();
然后依次枚举从每一个点,后面不可再出现这个点
参考代码:
// HDU5001 概率DP
#include<bits/stdc++.h>
using namespace std;
#define PI acos(-1.0)
#define eps 1e-8
#define mem(a,b) memset(a,b,sizeof a)
typedef long long LL;
typedef pair<int,int> P;
const int INF=0x3f3f3f3f;
const LL inf=0x3f3f3f3f3f3f3f3fLL;
const int maxn=2010;
int T,n,m,d,tot,u,v,head[maxn];
vector<int> vec[maxn];
double dp[maxn*5][55];//dp[i][j]表示走i步恰好到j点的概率  
int main()
{
	ios::sync_with_stdio(false);
	cin>>T;
	while(T--)
	{
		cin>>n>>m>>d;
		for(int i=0;i<=n;i++) vec[i].clear();
		for(int i=1;i<=m;i++)
		{
			cin>>u>>v;
			vec[u].push_back(v);
			vec[v].push_back(u);
		}
		for(int i=1;i<=n;i++) dp[0][i]=1.0/n;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=d;j++)
			{
				for(int k=1;k<=n;k++)
				{
					if(i==k) continue;
					dp[j][k]=0.0;
					for(int l=0;l<vec[k].size();l++)
					{
						if(i==vec[k][l]) continue;
						dp[j][k]+=dp[j-1][vec[k][l]]*1.0/vec[k].size(); 
					}
				}
			}
			double ans=0.0;
			for(int j=1;j<=n;j++) if(j!=i) ans+=dp[d][j];
			cout<<setiosflags(ios::fixed)<<setprecision(6)<<ans<<endl;
		}	
	}
	return 0;
}

  

HDU-5001 Walk (概率DP)

标签:number   概率   href   eof   content   ati   direction   efi   started   

原文地址:https://www.cnblogs.com/songorz/p/9651970.html

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