概率DP

dp[j][d] 表示不经过i点走d步到j的概率, dp[j][d]=sigma ( dp[k][d-1] * Probability )

ans = sigma ( dp[j][D] )

Walk

Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 401    Accepted Submission(s): 261

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
 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector> using namespace std; const int maxn=10010; int n,m,D;
vector<int> g[maxn];
double dp[55][maxn]; int main()
{
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d%d%d",&n,&m,&D);
for(int i=0;i<=n+1;i++) g[i].clear();
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
g[a].push_back(b);
g[b].push_back(a);
}
for(int i=1;i<=n;i++)
{
memset(dp,0,sizeof(dp));
for(int j=1;j<=n;j++)
{
if(i!=j) dp[j][0]=1.0/n;
} for(int d=1;d<=D;d++)
{
for(int j=1;j<=n;j++)
{
if(j==i) continue;
for(int k=0,sz=g[j].size();k<sz;k++)
{
int v=g[j][k];
if(v!=i) dp[j][d]+=dp[v][d-1]*(1./g[v].size());
}
}
} double ans=0.0;
for(int j=1;j<=n;j++)
{
if(i!=j) ans+=dp[j][D];
}
printf("%.10lf\n",ans);
}
}
return 0;
}

HDOJ 5001 Walk的更多相关文章

  1. BFS+贪心 HDOJ 5335 Walk Out

    题目传送门 /* 题意:求从(1, 1)走到(n, m)的二进制路径值最小 BFS+贪心:按照标程的作法,首先BFS搜索所有相邻0的位置,直到1出现.接下去从最靠近终点的1开始, 每一次走一步,不走回 ...

  2. 离散化+BFS HDOJ 4444 Walk

    题目传送门 /* 题意:问一个点到另一个点的最少转向次数. 坐标离散化+BFS:因为数据很大,先对坐标离散化后,三维(有方向的)BFS 关键理解坐标离散化,BFS部分可参考HDOJ_1728 */ # ...

  3. Hdu 5001 Walk 概率dp

    Walk Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5001 Desc ...

  4. 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 t ...

  5. HDU 5001 Walk (暴力、概率dp)

    Walk Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Sub ...

  6. HDU 5001 Walk

    解题思路:这是一道简单的概率dp,只要处理好相关的细节就可以了. dp[d][i]表示走d步时走到i的改概率,具体参考代码: #include<cstdio> #include<cs ...

  7. hdoj 5335 Walk Out

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5335 #include<stdio.h> #include<cstring> ...

  8. 【HDOJ】4579 Random Walk

    1. 题目描述一个人沿着一条长度为n个链行走,给出了每秒钟由i到j的概率($i,j \in [1,n]$).求从1开始走到n个时间的期望. 2. 基本思路显然是个DP.公式推导也相当容易.不妨设$dp ...

  9. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. actionBar-shareIcon 分享按钮的修改

    今天为了修改图库的分享按钮,进行了很多的尝试 1.寻找到了xml文件,如下 <?xml version="1.0" encoding="utf-8"?&g ...

  2. Android学习笔记进阶19之给图片加边框

    //设置颜色 public void setColour(int color){ co = color; } //设置边框宽度 public void setBorderWidth(int width ...

  3. org.omg.CORBA.MARSHAL: vmcid: SUN minor code: 211 completed: Maybe

    用weblogic 12c 测试 ejb3 import javax.naming.InitialContext; import javax.naming.NamingException; impor ...

  4. pycharm 注册 License server方式

    在输入框输入以下内容即可:http://idea.yangyusb.com

  5. 邮件协议与port

          电子邮箱的协议有SMTP.POP2.POP3.IMAP4等.都隶属于TCP/IP协议簇,默认状态下.分别通过TCPport25.110和143建立连接.针对不同的用途和功能,我们在邮件se ...

  6. 关于python的序列和矩阵运算的写法

    #其实下面是这样一个函数,传入的是obj_value,传出的是newobj_value.,, #这里的obj_value实际上是一个序列... for z in obj_value:          ...

  7. 学习 Perl(一) —— 安装及 hello world

    所谓存在的即是合理的,尤其适用于琳琅满目的编程语言界.每种编程语言在设计之初均只为解决特定领域的特定问题而生,没有语言擅长所有的领域能够完美地解决所有的问题. 这里推荐一本经典的 perl 入门书:P ...

  8. Codeforces #28 C.Bath Queue (概率dp)

    Codeforces Beta Round #28 (Codeforces format) 题目链接: http://codeforces.com/contest/28/problem/C 题意: 有 ...

  9. JS学习笔记 - 面向对象 - 原型

    <script> var arr1 = new Array(12, 55, 34, 78, 676); var arr2 = new Array(12, 33, 1) Array.prot ...

  10. Keepalived + Mysql 双主

    VIP 192.168.1.41 Master 192.168.1.42 Slave 192.168.1.43 .配置 yum -y install mysql-server chkconfig -- ...