hdu4579 Random Walk
来源:互联网 发布:seo外链接分享 编辑:程序博客网 时间:2024/05/18 20:07
题目:
Yuanfang is walking on a chain. The chain has n nodes numbered from 1 to n. Every second, he can move from node i to node j with probability:
c(i,j) is an element in a given parameter matrix which is n×m. (1 <= c(i, j) <= 9)
Yuanfang wants to know the expectation time for him to walk from node 1 to node n.
In each case, there are two integers n (2 <= n <= 50000), m (1 <= m <= 5), in the first line, meaning that there are n nodes and the parameter matrix is n×m . There are m integers in each of the next n lines which describe the parameter matrix .
The input ends with 0 0.
3 11115 21 22 13 22 31 30 0
6.948.75
思路:设dp[i]表示从i到n的步数期望值
那么dp[n]=0
列出n个变元的n个方程,这样就能用高斯消元解出来了。
代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<algorithm>#include<ctime>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<list>#include<numeric>using namespace std;#define LL long long#define ULL unsigned long long#define INF 0x3f3f3f3f#define mm(a,b) memset(a,b,sizeof(a))#define PP puts("*********************");template<class T> T f_abs(T a){ return a > 0 ? a : -a; }template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}// 0x3f3f3f3f3f3f3f3f// 0x3f3f3f3fconst int maxn=5e4+50;const double eps=1e-6;double c[maxn][6],p[maxn][11];double a[maxn][11],b[maxn];double dp[maxn];int main(){ int n,m; while(~scanf("%d%d",&n,&m)){ if(!n&&!m) break; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%lf",&c[i][j]); for(int i=1;i<n;i++){ double sum=1,s=0; for(int j=1;j<=m;j++) sum+=c[i][j]; for(int j=1;j<=m&&i-j>=1;j++){ p[i][m-j]=0.3*c[i][j]/sum; s+=p[i][m-j]; } for(int j=1;j<=m&&i+j<=n;j++){ p[i][m+j]=0.7*c[i][j]/sum; s+=p[i][m+j]; } p[i][m]=-s; b[i]=-1; } for(int i=1;i<n;i++){ int L=max(1,i-m); int R=min(n,i+m); for(int j=L;j<i;j++){ if(f_abs(p[i][m+j-i])<=eps) continue; double t=p[i][m+j-i]/a[j][1]; for(int k=1;k<=m+1&&j+k-1<=n;k++){ p[i][m+j-i+k-1]-=t*a[j][k]; } b[i]-=t*b[j]; } for(int j=1;j<=R-i+1;j++) a[i][j]=p[i][m+j-1]; } dp[n]=0; for(int i=n-1;i>=1;i--){ for(int j=2;j<=m+1&&i+j-1<=n;j++) b[i]-=a[i][j]*dp[i+j-1]; dp[i]=b[i]/a[i][1]; } printf("%.2f\n",dp[1]); } return 0;}
阅读全文
0 0
- hdu4579 Random Walk
- Random Walk
- random walk
- random walk
- hdu4487 Maximum Random Walk
- D - Maximum Random Walk
- About Random Walk
- random walk DEMO
- Random Walk分割算法
- 随机漫步(random walk)
- Hdu 4487 Maximum Random Walk
- hdu 4487 Maximum Random Walk
- hdu 4487 Maximum Random Walk
- Program work 2. Random walk
- POJ 2714 : Random Walk - 贪心
- HDU 4487 Maximum Random Walk
- 《挑战》例题4.1 Random Walk
- 挑战 Random Walk[高斯消元&&期望]
- 为什么说HTTP协议是无状态的,以及session和cookie的区别。转载自leelyn168
- 关于Socket和NetWorkstream介绍
- 讯飞AIUI集成语音语义的21003错误
- 每日英语阅读(三十九)
- K
- hdu4579 Random Walk
- 【Python+OpenCV】实现RGB转HSI
- 过河[NOIP2005]
- 斯坦福 机器学习-第一章监督学习
- macOS 10.12/10.13 字体美化(华文黑体删除)
- kill 杀不死进程的原因
- JavaMelody监控tomcat部署
- HDU-1281-棋盘游戏 [二分匹配][关键匹配]
- 读书:鲁迅的《呐喊》和《彷徨》