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 <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
//#define LL __int64
#define LL long long
#define eps 1e-9
#define PI acos(-1.0)
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 55;

vector <int> edge[55];

double dp[55][10010];
int n,m,D;

int main()
{
	int test,u,v;
	scanf("%d",&test);
	while(test--)
	{
		scanf("%d %d %d",&n,&m,&D);
		D++;

		for(int i = 1; i <= n; i++)
			edge[i].clear();
		for(int i = 1; i <= m; i++)
		{
			scanf("%d %d",&u,&v);
			edge[u].push_back(v);
			edge[v].push_back(u);
		}

		for(int i = 1; i <= n; i++)
		{
			memset(dp,0,sizeof(dp));

			for(int d = 1; d <= D; d++)
			{
				if(d == 1)
				{
					for(int j = 1; j <= n; j++)
					{
						if(j != i)
							dp[j][d] = 1.0/n;
					}
				}
				else
				{
					for(int j = 1; j <= n; j++)
					{
						if(j != i)
						{
							for(int g = 0; g < (int)edge[j].size(); g++)
							{
								int k = edge[j][g];
								if(k != i)
									dp[j][d] += dp[k][d-1]*1.0/edge[k].size();
							}
						}
					}
				}
			}
			double ans = 0;
			for(int j = 1; j <= n; j++)
			{
				if(j != i)
					ans += dp[j][D];
			}
			printf("%.10lf\n",ans);
		}
	}
	return 0;
}
时间: 2024-10-22 11:41:28

hdu 5001 Walk(概率)的相关文章

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 概率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(鞍山网络赛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: 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 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

[概率dp] hdu 5001 Walk

题意:n个点(1~n),m条边,走k次,双向边,每次随机走一条路,起点也随机,问不走到各个点的概率是多少. 思路: 概率dp[i][j][k] 前i步 走到j 走不到k的概率. 那么状态转移就是 j能走到的点,传递所有dp[i][j][k]的值乘上概率. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #include"

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