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.
 

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
 

题意大致为一张双向图相同概率一个点进入,任意走,问走完d步所有点没走过的概率。要是你欲先求出所有点能走过的概率,然后1-它输出的话,你就走远了。。。

正解应该是刚开始每个点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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 4个月宝宝流鼻血怎么办 20个月宝宝发烧流鼻血怎么办 60天宝宝老鼻塞怎么办 10个月宝宝头被撞到流鼻血怎么办 狗狗受凉吐了怎么办 狗狗咳嗽流鼻涕一直不好怎么办 宝宝感冒咳嗽流鼻涕发烧怎么办 狗狗感冒咳嗽流鼻涕怎么办 9岁儿童咳嗽鼻塞怎么办 三个月大的狗狗流鼻涕怎么办 3个月小狗干呕流鼻涕怎么办 狗狗流鼻涕怎么办有浓 小狗狗感冒了怎么办呢 狗狗一直擤鼻涕怎么办 小狗感冒流黄鼻涕怎么办 六个月宝宝鼻塞流鼻涕怎么办 小狗吃太多吐了怎么办 狗狗晕车一直吐怎么办 狗狗已经晕车了怎么办 狗狗得犬瘟怎么办 泰迪坐车吐了怎么办 小孩感冒流鼻涕带血怎么办 孩子鼻子流鼻涕有血丝怎么办 鼻子过敏流鼻涕有血丝怎么办 孕妇感冒头痛鼻涕带血怎么办 孕妇感冒鼻塞鼻涕带血怎么办 孕晚期感冒流鼻涕打喷嚏怎么办 孕晚期感冒鼻塞流鼻涕怎么办 怀孕初期鼻涕一直流怎么办 孩子一直流鼻水怎么办 9个月宝宝流鼻涕怎么办 8个月婴儿流鼻涕怎么办 3岁宝宝鼻塞咳嗽怎么办 又感冒又咳嗽了怎么办 鼻塞有一个月了怎么办 感冒一直流清水鼻涕怎么办 孩子受凉流清水鼻涕怎么办 一遇冷空气就打喷嚏流鼻涕怎么办 打喷嚏鼻塞流清鼻涕怎么办 哺乳期鼻子不通气有鼻涕怎么办 宝宝热伤风流清鼻涕怎么办