HDU-5001 Walk (概率DP)
阿新 • • 發佈:2018-09-15
ror lines set ger err accept start scrip time
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.
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.
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.
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 Recommend hujie 題解:給你N個點,M條邊(無向),然後可以走d步,讓你求恰好不走i點(i 1~N)的概率 ; 概率DP;dp[i][j]:表示走i步,到達j點的概率;對於當前點,它一定是由其相鄰點走過來的,則這個點的概率為這些和他相鄰的點的概率和 : dp[j][k]+=dp[j-1][vec[k][l]]*1.0/vec[k].size(); 然後依次枚舉從每一個點,後面不可再出現這個點 參考代碼:
// HDU5001 概率DP #include<bits/stdc++.h> using namespace std; #define PI acos(-1.0) #define eps 1e-8 #define mem(a,b) memset(a,b,sizeof a) typedef long long LL; typedef pair<int,int> P; const int INF=0x3f3f3f3f; const LL inf=0x3f3f3f3f3f3f3f3fLL; const int maxn=2010; int T,n,m,d,tot,u,v,head[maxn]; vector<int> vec[maxn]; double dp[maxn*5][55];//dp[i][j]表示走i步恰好到j點的概率 int main() { ios::sync_with_stdio(false); cin>>T; while(T--) { cin>>n>>m>>d; for(int i=0;i<=n;i++) vec[i].clear(); for(int i=1;i<=m;i++) { cin>>u>>v; vec[u].push_back(v); vec[v].push_back(u); } for(int i=1;i<=n;i++) dp[0][i]=1.0/n; for(int i=1;i<=n;i++) { for(int j=1;j<=d;j++) { for(int k=1;k<=n;k++) { if(i==k) continue; dp[j][k]=0.0; for(int l=0;l<vec[k].size();l++) { if(i==vec[k][l]) continue; dp[j][k]+=dp[j-1][vec[k][l]]*1.0/vec[k].size(); } } } double ans=0.0; for(int j=1;j<=n;j++) if(j!=i) ans+=dp[d][j]; cout<<setiosflags(ios::fixed)<<setprecision(6)<<ans<<endl; } } return 0; }
HDU-5001 Walk (概率DP)