Walk HDU - 5001

版权声明:小白一个,欢迎各位指错。 https://blog.csdn.net/qq_36424540/article/details/82759834

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

当时还想着记忆化搜索,现在发现根本不行,因为他不是一棵树,他不会是只到达一次,他有可能从别的路径,完全相同的起点,相同的距离,所以我们没有办法记忆化。

所以我们来想想,如果我们把某个结点去掉,让所有的合法路径都在其他的路上跑就可以了。 还是有点拓扑的感觉,我们去枚举 d,不断的向前推进,只要不合法,我们就会放弃这个点的更新,所以我们想象到d的时候,就像是分支,到了叶子节点,中间的不合法的路径都被剪枝了。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)

const int M=2500;
const int N=60;
vector<int> vec[N];

void add_edge(int u,int v) {
	vec[u].push_back(v);
	vec[v].push_back(u);
}
/*
2
5 10 10
1 2
2 3
3 4
4 5
1 5
2 4
3 5
2 5
1 4
1 3
*/
double dp[N][10010];

int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		int n,m,d;
		scanf("%d %d %d",&n,&m,&d);

		for(int i=1; i<=n; i++){
			vec[i].clear();
			for(int j=1; j<=d; j++)dp[i][j]=0;
		}

		rep(i,0,m) {
			int u,v;
			scanf("%d %d",&u,&v);
			add_edge(u,v);
		}
	
	
		rep(t,1,n+1) {
			
			rep(i,0,n+1)rep(j,0,d+1)dp[i][j]=0;
			
			rep(i,1,n+1){
				if(i==t)continue;
				dp[i][0]=1.0/n;
			}			
			
			rep(D,1,d+1){
				rep(i,1,n+1){
					if(i==t)continue;
					int sz=vec[i].size();
					rep(j,0,sz){
						int v=vec[i][j];
						if(v==t)continue;
						dp[v][D]+=dp[i][D-1]/sz;
					}
				}
			}
			double ans=0;
			for(int i=1;i<=n;i++){
				ans+=dp[i][d];
			}
			printf("%.10f\n",ans);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/82759834
今日推荐