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
 
题目大意:给一张无根无向图(n个节点,m条边),每一步走哪一个儿子节点的概率相同(以哪个点为起点的概率也相同)。对于每个点,找出走完d步后走不到该点的概率。
题目分析:定义dp(s,u)表示不经过节点i(1<=i<=n)走了s步时,到达u点的概率。则根据加法原理,dp(s,son)=sum(dp(s-1,u)*f),其中 f 是u走到其儿子son的概率,f=1.0/(u的儿子个数)。题目要求分别输出不经过节点 i (1<=i<=n) 的答案,则DP n次即可。对于每次DP,根据加法原理,答案为ans=sum(dp(d,j)),其中,j不等于i。
 
注意:第一步是从起点开始的,而选择起点的过程不能算做一步。
 
代码如下:
# include<iostream>
# include<cstdio>
# include<vector>
# include<cstring>
# include<algorithm>
using namespace std;
vector<int>v[55];
int n,m,d,vis[10005][55];
double dp[10005][55];
void solve()
{
for(int i=1;i<=n;++i){
memset(dp,0,sizeof(dp));
for(int j=1;j<=n;++j)
dp[0][j]=1.0/n;
for(int j=1;j<=d;++j){
for(int start=1;start<=n;++start){
if(i==start)
continue;
int l=v[start].size();
for(int k=0;k<l;++k)
dp[j][v[start][k]]+=dp[j-1][start]*1.0/l;
}
}
double ans=0.0;
for(int j=1;j<=n;++j)
if(j!=i)
ans+=dp[d][j];
printf("%.10lf\n",ans);
}
}
int main()
{
int T,a,b;
vector<int>::iterator it;
scanf("%d",&T);
while(T--)
{
memset(vis,0,sizeof(vis));
scanf("%d%d%d",&n,&m,&d);
for(int i=1;i<=n;++i)
v[i].clear();
while(m--)
{
scanf("%d%d",&a,&b);
it=find(v[a].begin(),v[a].end(),b);
if(it==v[a].end())
v[a].push_back(b);
it=find(v[b].begin(),v[b].end(),a);
if(it==v[b].end())
v[b].push_back(a);
}
solve();
}
return 0;
}

  

随机推荐

  1. Cocos2dx 3.12 在AndroidStudio上编译配置

    转载请标明出处:http://www.cnblogs.com/studweijun/p/5805576.html SDK,NDK,ANT的配置请看这里http://www.cnblogs.com/st ...

  2. 01python算法--算法和数据结构是什么鬼?

    我不想直接拷贝google 上面所有对算法的解释.所以我想怎么说就怎么说了,QAQ 1:什么是程序? 解决问题的范式 2:什么是问题? 程序输入与输出之间的联系 3:什么是算法: 算法就是解决问题的思 ...

  3. Portal for ArcGIS上传shp文件中文乱码可能情况

    环境: windows Server 2008:Portal for ArcGIS 10.2:ArcGIS for Server 10.2:PostgreSQL 9.2:ArcGIS for Desk ...

  4. mysql:innodb monitor(show engine innodb status)探秘

    在旧的版本里面是show innodb status命令,新版本后改动了一些:show engine innodb status; 我们最熟悉的,应当就是show innodb status命令,可以 ...

  5. common-pool2 使用

    common-pool2提供了3中对象池管理方式,它们的使用方式基本一样,这里以GenericObjectPool对象池为例介绍其使用方式,一般实现自己的对象池需要经过2个步骤 1.实现PooledO ...

  6. 了解javascript中的this --实例篇

    对javascript this的赋值有了深一层的理解后,看一下比较复杂的情况,this的应用篇参考<对javascript this的理解>. #demo1 var name=" ...

  7. .NET的类型层次查看工具

    上周为了快速了解一个.NET的库而需要查看其类型层次.假如要在文章中表示一个类型层次,还是用文本比较舒服,截图始终是不方便.Reflector虽然能够显示类型层次,但我无法方便的把显示出来的类型层次转 ...

  8. CentOS相关引导文件杂摘

    1,EFI文件

  9. [LeetCode] 310. Minimum Height Trees 解题思路

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  10. centos 7 卸载 mariadb 安装mysql

    1,卸载mariadbsystemctl stop mariadbrpm -qa | grep mariadbrpm -e --nodeps mariadb-5.5.52-1.el7.x86_64rp ...