首页 > 代码库 > HDU 5001 Walk (暴力、概率dp)
HDU 5001 Walk (暴力、概率dp)
Walk
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 266 Accepted Submission(s): 183 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.
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.
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.
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
Recommend
hujie | We have carefully selected several similar problems for you: 5017 5016 5015 5014 5013
设dp[j][d]表示不能经过i点走了d步到达j点的概率。那么dp[j][d] = ∑ dp[k][d-1]/edge[k].size()。那么不经过i点的概率为∑dp[j][D]。(转自:http://blog.csdn.net/u013081425/article/details/39254337) (我的代码是dp[d][j])
每次都去掉一个点求出到达 其他点的概率就是不能到达这个点的概率。(转自:http://blog.csdn.net/xu12110501127/article/details/39254403)
1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<map> 9 #include<string>10 11 #define N 5512 #define M 1513 #define mod 100000000714 #define p 1000000715 #define mod2 10000000016 #define ll long long17 #define LL long long18 #define maxi(a,b) (a)>(b)? (a) : (b)19 #define mini(a,b) (a)<(b)? (a) : (b)20 21 using namespace std;22 23 int T;24 int n,m,d;25 vector<int>bian[N];26 int cnt[N];27 double dp[10005][N];28 double re;29 30 void ini()31 {32 int i;33 int x,y;34 memset(cnt,0,sizeof(cnt));35 scanf("%d%d%d",&n,&m,&d);36 for(i=1;i<=n;i++){37 bian[i].clear();38 }39 while(m--){40 scanf("%d%d",&x,&y);41 bian[x].push_back(y);42 bian[y].push_back(x);43 }44 for(i=1;i<=n;i++){45 cnt[i]=bian[i].size();46 }47 }48 49 void solve()50 {51 int q,o,i;52 for(q=1;q<=n;q++)53 {54 re=0;55 memset(dp,0,sizeof(dp));56 for(i=1;i<=n;i++){57 if(i==q) continue;58 dp[0][i]=1.0/n;59 }60 61 for(o=1;o<=d;o++){62 for(i=1;i<=n;i++){63 if(i==q) continue;64 for(vector<int>::iterator it=bian[i].begin();it!=bian[i].end();it++){65 dp[o][i]+=dp[o-1][*it]/cnt[*it];66 }67 }68 }69 70 for(i=1;i<=n;i++){71 if(i==p) continue;72 re+=dp[d][i];73 }74 printf("%.10f\n",re);75 }76 }77 78 void out()79 {80 //printf("%I64d\n",ans);81 }82 83 int main()84 {85 //freopen("data.in","r",stdin);86 scanf("%d",&T);87 //for(int cnt=1;cnt<=T;cnt++)88 while(T--)89 //while(scanf("%I64d%I64d",&n,&m)!=EOF)90 {91 ini();92 solve();93 // out();94 }95 96 return 0;97 }
HDU 5001 Walk (暴力、概率dp)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。