Description I used to think I could is 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'll travel to a adjacent node with the same probability in the next step. I'll pick up the start node randomly (each node in the graph have 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'll make me unhappy. So I wonder to each node, what's the probability that my path doesn ' t contain it. Input The first line contains an integer T, denoting the number of the 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 an d The number of steps respectively. Then M. lines follows, each containing, integers a and B, denoting there are 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 is 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 would be the accepted if its absolute error doesn ' t exceed 1e-5. Sample Input
Sample Output
|
The main topic: In a picture, the protagonist randomly selects a point as the starting point, and then each step is randomly selected adjacent points for access, a total of walk D step. The probability that each point has not been accessed.
Probability DP entry, definition DP[U][I][J]: The probability of a J-step falling at I point and never passing u point. Finally, for each point, the probability of never passing it is dp[u][*][d].
In the actual operation, the DP array can be changed into two-dimensional, that is, dp[i][j].
#include <stdio.h>#include<stdlib.h>#include<string.h>using namespacestd;Const intMAXN = -;inthead[maxn],cur;structedge{intto ; intNext;} EDGE[MAXN*2];voidinit () {cur=0; memset (Head,-1,sizeof(head));}voidAddedge (intUintv) {edge[cur].to=v; Edge[cur].next=Head[u]; Head[u]= cur++;}Doubledp[maxn][10005];DoubleANS[MAXN];intDu[maxn],n;voidCAL_DP (intXintd) { for(intu=1; u<=n; u++) {Dp[u][d]=0; if(u==x)Continue; for(intE=head[u]; e!=-1; E=Edge[e].next) { intv =edge[e].to; if(v==x)Continue; DP[U][D]+ = dp[v][d-1]/Du[v]; } }}intMain () {intt,u,v,m,d; scanf ("%d",&T); while(t--) {init (); Memset (Du,0,sizeof(du)); scanf ("%d%d%d",&n,&m,&d); for(intI=0; i<m; i++) {scanf ("%d%d",&u,&v); Addedge (U,V); Addedge (V,u); Du[u]++; DU[V]++; } for(intI=1; i<=n; i++) {dp[i][0] =1.0/N; Ans[i]=0; } for(intu=1; u<=n; u++) { for(intj=1; j<=d; J + +) CAL_DP (U,J); for(intv=1; v<=n;v++) Ans[u]+=Dp[v][d]; } for(intI=1; i<=n; i++) {printf ("%.10lf\n", Ans[i]); } }}
HDU 5001 Walk probability DP entry