2014鞍山網路賽 E題||hdu 5001 概率dp
http://acm.hdu.edu.cn/showproblem.php?pid=5001
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
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
解題思路: dp[i][j][k]表示,i代表當前 第i步,j代表當前走到第j個節點,k是沒有路過k,整體是 走到第i步恰好在j節點,這個過程中沒有路過k的概率
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
double a[3][55][55],ans[55];
int mp[55][55],cnt[55];
int T,n,m,d;
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&d);
memset(mp,0,sizeof(mp));
memset(a,0,sizeof(a));
memset(cnt,0,sizeof(cnt));
for(int i=0; i<m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
mp[u][v]=mp[v][u]=1;
cnt[u]++;
cnt[v]++;
}
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if(i!=j)
a[1][i][j]=1.0/n;
else
a[1][i][j]=0;
for(int i=1; i<=d; i++)
{
memset(a[(i+1)%2],0,sizeof(a[(i+1)%2]));
for(int j=1; j<=n; j++)
for(int k=1; k<=n; k++)
if(mp[j][k])
for(int p=1; p<=n; p++)
if(k!=p)
a[(i+1)%2][k][p]+=a[i%2][j][p]*(1.0/cnt[j]);
else
a[(i+1)%2][k][p]=0;
}
for(int i=1;i<=n;i++)
{
ans[i]=0;
for(int j=1;j<=n;j++)
ans[i]+=a[(d+1)%2][j][i];
}
for(int i=1;i<=n;i++)
printf("%.10lf\n",ans[i]);
}
return 0;
}
相關文章
- ACM/ICPC2014鞍山現場賽E hdu5074Hatsune MikuACM
- HDU 3853 LOOPS(概率dp)OOP
- ACM/ICPM2014鞍山現場賽D Galaxy (HDU 5073)ACM
- hdu 4089||2011年北京現場賽I題 概率dp(公式不好推)公式
- 2014上海網路賽1004||hdu5045 contest【狀態壓縮dp】
- 概率DP入門題
- 2014北京網路賽1006||hdu5037 思維題
- HDU 4326Game(比較難理解的概率dp)GAM
- 2014上海網路賽1004||hdu5045 二分圖的最佳匹配 或 狀態壓縮dp
- ACM-ICPC 2018 南京賽區網路預賽__E AC Challenge【狀態壓縮+DP】ACM
- 2013杭州網路賽C題HDU 4640(模擬)
- 2014廣州網路賽1003||hdu 5024 搜尋
- 2014北京網路賽1007||hdu5038 模擬
- 2014西安網路賽1006||hdu5012 bfs
- 2013長沙網路賽 E題(水題 有點小bug)
- 2014廣州網路賽1004||hdu5025 分層最短路
- 2014西安網路賽1009||hdu5015 矩陣矩陣
- hdu5452 || 瀋陽網路賽1003題 最近公共祖先問題
- 2014西安網路賽1008||hdu5014 二進位制
- HDU 3853 LOOPS:期望dp【網格型】OOP
- 概率DP總結 by kuangbin
- SGU 495 Kids and Prizes:期望dp / 概率dp / 推公式公式
- codeforces 148 D 概率dp
- hdu5489 ||2015合肥網路賽1006 dp+離散化樹狀陣列優化陣列優化
- hdu5445 || 2015長春網路賽1009題 多重揹包問題
- hdu 3507 斜率優化DP入門題優化
- 2014廣州網路賽1002||hdu5023 線段樹&&狀態壓縮
- 2013成都網路賽1004題HDU 4731Minimum palindrome (思維題目)
- POJ 3744 概率dp+矩陣矩陣
- 演算法題——投籃比賽獲勝概率問題演算法
- 牛客練習賽74 E CCA的期望(算概率的技巧+floyd處理)
- 2014年北京師範大學新生程式設計競賽網路賽程式設計
- HDU 4669 Mutiples on a circle (DP , 統計)
- codeforces 148 D Bag of mice(概率dp)
- Codeforces 351B Jeff and Furik:概率 + 逆序對【結論題 or dp】
- hdu 4292 網路最大流
- HDU5441 Travel (2015年長春網路賽,並查集)並查集
- HDU4722Good Numbers熱身賽2 1007題(思維題 不用數位dp照樣可以做的)Go