HDU - 5001 Walk(概率dp+记忆化搜索)
Walk
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.
InputThe 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.OutputFor 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 第一道比赛出的概率dp。找好状态记忆化搜索即可。
#include<bits/stdc++.h>
#define MAX 55
using namespace std;
typedef long long ll; int n,m,k;
double ans;
double dp[MAX][];
vector<int> v[MAX];
double dfs(int f,int x,int s){
int i;
if(s>=k) return ;
if(dp[x][s]>-) return dp[x][s];
int xx=v[x].size();
double cnt=;
for(i=;i<v[x].size();i++){
if(v[x][i]==f) continue;
cnt+=dfs(f,v[x][i],s+)*(1.0/xx);
}
dp[x][s]=cnt;
return cnt;
}
int main()
{
int t,i,j;
int x,y;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&k);
for(i=;i<=n;i++){
v[i].clear();
}
for(i=;i<=m;i++){
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
for(i=;i<=n;i++){
ans=;
memset(dp,-,sizeof(dp));
for(j=;j<=n;j++){
if(i==j) continue;
ans+=dfs(i,j,)*(1.0/n);
}
printf("%.10f\n",ans);
}
}
return ;
}
HDU - 5001 Walk(概率dp+记忆化搜索)的更多相关文章
- HDU 5001 概率DP || 记忆化搜索
2014 ACM/ICPC Asia Regional Anshan Online 给N个点,M条边组成的图,每一步能够从一个点走到相邻任一点,概率同样,问D步后没走到过每一个点的概率 概率DP 測 ...
- Codeforces 148D Bag of mice:概率dp 记忆化搜索
题目链接:http://codeforces.com/problemset/problem/148/D 题意: 一个袋子中有w只白老鼠,b只黑老鼠. 公主和龙轮流从袋子里随机抓一只老鼠出来,不放回,公 ...
- CodeForces 398B 概率DP 记忆化搜索
题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...
- Hdu 5001 Walk 概率dp
Walk Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5001 Desc ...
- hdu3559 Frost Chain (概率dp+记忆化搜索)
Problem Description In the unimaginable popular DotA game, the hero Lich has a wonderful skill: Fros ...
- 【bzoj5123】[Lydsy12月赛]线段树的匹配 树形dp+记忆化搜索
题目描述 求一棵 $[1,n]$ 的线段树的最大匹配数目与方案数. $n\le 10^{18}$ 题解 树形dp+记忆化搜索 设 $f[l][r]$ 表示根节点为 $[l,r]$ 的线段树,匹配选择根 ...
- 【BZOJ】1415 [Noi2005]聪聪和可可 期望DP+记忆化搜索
[题意]给定无向图,聪聪和可可各自位于一点,可可每单位时间随机向周围走一步或停留,聪聪每单位时间追两步(先走),问追到可可的期望时间.n<=1000. [算法]期望DP+记忆化搜索 [题解]首先 ...
- !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)
题意:有一个n*n的格子.每一个格子里有不同数量的食物,老鼠从(0,0)開始走.每次下一步仅仅能走到比当前格子食物多的格子.有水平和垂直四个方向,每一步最多走k格,求老鼠能吃到的最多的食物. 分析: ...
- [题解](树形dp/记忆化搜索)luogu_P1040_加分二叉树
树形dp/记忆化搜索 首先可以看出树形dp,因为第一个问题并不需要知道子树的样子, 然而第二个输出前序遍历,必须知道每个子树的根节点,需要在树形dp过程中记录,递归输出 那么如何求最大加分树——根据中 ...
随机推荐
- sort()函数到底是怎样进行数字排序的
很多人会用sort(),并不见得知道它具体是怎样给数字排序的.其实不知道也行,会用就可以,感兴趣的可以来看看. var numberArray = [2,4,1,3]; numberArray.sor ...
- 我的Android进阶之旅------>温习Sqlite3的常用操作
前言;今天要写一个应用来调节系统的Brightness值,来改变系统的背光亮度.由于刚开始些的时候没有考虑Brightness的最小值,直接托动SeekBar到最小值(为0).瞬间,屏幕变成全黑,失败 ...
- Versions 崩溃(Mac升级OS X Yonsemite 10.10)
今天兴冲冲的升级到了OS X Yonsemite 10.10,结果发现SVN工具不能用了,于是找到一个暂时的解决的方法 1.打开目录~/.subversion/servers 2.在[global] ...
- mysql服务突然不能启动
mysql之前一直用的好好的,今天突然不能启动.前几天改过my.ini文件,在[mysqld]下添加了default-character-set=utf8.查看日志发现是添加的那句话的错误,删除.并且 ...
- LightOJ - 1248 Dice (III) —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1248 1248 - Dice (III) PDF (English) Statistics Forum Tim ...
- POJ 3744 Scout YYF I:概率dp
题目链接:http://poj.org/problem?id=3744 题意: 有n个地雷,位置为pos[i]. 在每个位置,你向前走一步的概率为p,向前走两步的概率为1-p. 你的初始位置为1. 问 ...
- jsp九大内置对象和其作用详解
jsp九大内置对象和其作用详解 JSP中一共预先定义了9个这样的对象,分别为:request.response.session.application.out.pagecontext.config.p ...
- PSPnet:Pyramid Scene Parsing Network——作者认为现有模型由于没有引入足够的上下文信息及不同感受野下的全局信息而存在分割出现错误的情景,于是,提出了使用global-scence-level的信息的pspnet
from:https://blog.csdn.net/bea_tree/article/details/56678560 2017年02月23日 19:28:25 阅读数:6094 首先声明,文末彩蛋 ...
- Java微信公众平台开发_03_消息管理之被动回复消息
GitHub源码:https://github.com/shirayner/weixin_gz 一.本节要点 1.回调url 上一节,我们启用服务器配置的时候,填写了一个服务器地址(url),如下图, ...
- c++类之间的关系
我们知道,表达方式分为很多种,对于同一种事物,比如爱情,画家用图画和色彩表达爱恋:音乐家用音符和节奏表达喜爱之情,作家用文字表现爱慕. 而程序员怎么办? 程序员构建类,用类来表达单身之苦.因此,类就是 ...