首页 > 代码库 > 2014鞍山ACM网络赛 HDU5001 Walk

2014鞍山ACM网络赛 HDU5001 Walk

 

 

 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
25 10 1001 22 33 44 51 52 43 52 51 41 310 10 101 22 33 44 55 66 77 88 99 104 9
 

 

Sample Output
0.00000000000.00000000000.00000000000.00000000000.00000000000.69933179670.58642849520.44408608210.22758969910.42940745910.48510487420.48960188420.45250442500.34065674830.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 0x3f3f3f3f15 #define PI acos(-1.0)16 #define eps 1e-617 #define LL long long18 #define MEM(a,b) memset(a,b,sizeof(a))19 #define PB push_back20 #define MP make_pair21 #define PQ priority_queue22 #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 271 5 10 272 1 273 2 374 3 475 4 576 1 577 2 478 3 579 2 580 1 481 1 382 183 3 4 484 1 285 2 386 3 187 */

 

2014鞍山ACM网络赛 HDU5001 Walk