Walk
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 543 Accepted Submission(s): 344
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
2014 ACM/ICPC Asia Regional Anshan Online
总结:在做这道题目的时候刚开始自己一直纠结于,如何处理这种情况 A,B,C 当走第二部的时候,有A到B的那部分概率就不能由B走到A了,,这个问题纠结了好长时间,可能是因为自己以前没有做过概率DP方面的东西吧。 看了下别人的题解才知道, 只需要枚举出来一个点在走的时候不把它作为起点就一切OK了,, 这个时候才明白过来, 反正我们算的是枚举的这个点的概率,其它点之间爱咋搞咋搞。
代码,, 一把辛酸泪,,
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<ctime> 5 #include<cstring> 6 #include<string> 7 #include<map> 8 #include<set> 9 #include<cmath> 10 #include<stack> 11 #include<queue> 12 #include<vector> 13 #include<algorithm> 14 #define inf 0x3f3f3f3f 15 #define PI acos(-1.0) 16 #define eps 1e-6 17 #define LL long long 18 #define MEM(a,b) memset(a,b,sizeof(a)) 19 #define PB push_back 20 #define MP make_pair 21 #define PQ priority_queue 22 #define IN freopen("in.txt","r",stdin); 23 #define OUT freopen("out.txt","w",stdout); 24 #define BUG printf("bug************bug************bug\n"); 25 26 using namespace std; 27 28 const int N=60; 29 double f[2][N][N]; 30 double p[N]; 31 vector <int> G[N]; 32 33 int main() 34 { 35 int T,n,m,d,x,y; 36 scanf("%d",&T); 37 while (T--){ 38 memset(p,0,sizeof(p)); 39 memset(f,0,sizeof(f)); 40 scanf("%d%d%d",&n,&m,&d); 41 for (int i=1;i<=n;i++) G[i].clear(); 42 for (int i=1;i<=m;i++){ 43 scanf("%d%d",&x,&y); 44 G[x].push_back(y); 45 G[y].push_back(x); 46 } 47 for (int i=1;i<=n;i++) 48 for (int j=1;j<=n;j++) f[0][i][j]=1.0/n; 49 50 for (int k=0;k<d;k++){ 51 memset(f[(k+1)&1],0,sizeof(f[(k+1)&1])); 52 for (int i=1;i<=n;i++){ 53 for (int j=1;j<=n;j++){ 54 if (i==j) continue; 55 int u=G[j].size(); 56 for (int v=0;v<u;v++){ 57 int x=G[j][v]; 58 f[(k+1)&1][i][x]+=f[k&1][i][j]/u; 59 } 60 } 61 p[i]+=f[(k+1)&1][i][i]; 62 } 63 } 64 for (int i=1;i<=n;i++) 65 printf("%0.10lf\n",1-p[i]-1.0/n); 66 } 67 } 68 69 /* 70 2 71 5 10 2 72 1 2 73 2 3 74 3 4 75 4 5 76 1 5 77 2 4 78 3 5 79 2 5 80 1 4 81 1 3 82 1 83 3 4 4 84 1 2 85 2 3 86 3 1 87 */