HDU P5001 Walk
来源:互联网 发布:在ubuntu上卸载cuda6.5 编辑:程序博客网 时间:2024/06/07 17:32
2014区域网络赛鞍山校区上的第五题:
Walk
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 183 Accepted Submission(s): 127
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
正解应该是刚开始每个点1/n的初始概率,然后删除I点,之后求出其余点能走到的概率,能走到的概率总和即I点不能走到的概率,然后注意到任意两点直接走到的概率可以形成一个矩阵,走了d步相当于初始概率形成的矩阵*概率矩阵^d。 故可以用矩阵快速幂来做。
由于时限较宽,其余方法还有很多,若暂时看不懂本题含义,可以先去看这道题的概率DP题解,其实思想是一样的。
代码:
#include<iostream>#include<cstdio>#include<cstdlib>#include<ctime>#include<string>#include<cstring>#include<algorithm>#include<fstream>#include<queue>#include<stack> #include<vector>#include<cmath>#include<iomanip>#define rep(i,n) for(i=1;i<=n;i++)#define MM(a,t) memset(a,t,sizeof(a))#define INF 1e9typedef long long ll;#define mod 1000000007using namespace std;int n,m,d;vector<int> eg[100];struct matrix{double a[100][100];}pan,st,npan;matrix mul(matrix a1,matrix a2,int i1,int i2){int i,j,k;matrix c;rep(i,i1) rep(j,i2){ c.a[i][j]=0; rep(k,i2){ c.a[i][j]+=a1.a[i][k]*1.0*a2.a[k][j]; } } return c;}void qmi(int nn){int j,k;while(nn){ if(nn%2==1) st=mul(st,npan,1,n); npan=mul(npan,npan,n,n); nn=(nn>>1);}}int main(){int i,j,k,T; scanf("%d",&T); while(T--){ scanf("%d%d%d",&n,&m,&d); rep(i,n) eg[i].clear(); MM(pan.a,0); rep(i,m){ int s,e; scanf("%d%d",&s,&e); eg[s].push_back(e); eg[e].push_back(s); } rep(i,n){ int sz=eg[i].size(); for(j=0;j<sz;j++) pan.a[i][eg[i][j]]=1.0/sz; } rep(i,n){ double res=0; rep(j,n) st.a[1][j]=1.0/n; MM(npan.a,0); rep(j,n) rep(k,n) if(j!=i && k!=i) npan.a[j][k]=pan.a[j][k]; qmi(d); rep(j,n) res+=st.a[1][j]; printf("%.8lf\n",res); } } return 0;}
0 0
- HDU P5001 Walk
- HDU Walk
- hdu 4444 Walk
- hdu 4444 Walk
- HDU 4444 - Walk
- hdu 2323 Honeycomb Walk
- hdu 5001 Walk(概率)
- HDU 5001 Walk (暴力)
- HDU - 5001 Walk
- hdu 5001 walk
- HDU 5001 Walk
- hdu 5001 Walk
- HDU 5001 Walk
- HDU 5001 Walk
- HDU 5001 Walk
- hdu 5001 Walk
- hdu 5335 Walk Out
- HDU 5335 Walk Out
- HDU 2846 Repository
- 字符串分割
- Manacher(枚举位置)+uva11888
- 一个无处不在的工厂方法
- poj 1007 DNA Sorting (求逆序数)
- HDU P5001 Walk
- 关于寄存器ESP和EBP的一些理解
- Java中super的几种用法并与this的区别,this的特殊用法
- Mongodb 安装配置,启动关闭
- 第十二章、正規表示法與文件格式化處理
- MATLAB: 如何读取txt中的文件 如何将数据批量写入数据 文件名递增
- bzoj1009 GT考试 KMP+矩阵优化DP
- 转载:PostgreSQL时间线的实现
- Codeforces Round #266 (Div. 2) B & C