Random Walk

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65535/65536 K (Java/Others)
Total Submission(s): 81    Accepted Submission(s): 35

Problem Description
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.

 
Input
There are no more than 10 test cases.
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.
 
Output
For each case, output the expectation time for Yuanfang to walk from node 1 to node n in one line. The answer should be rounded to 2 digits after decimal point.
 
Sample Input
3 1
1
1
1
5 2
1 2
2 1
3 2
2 3
1 3
0 0
 
Sample Output
6.94
8.75
 
Source
 
 
 
 
 
这题就是列出方程。
 
然后高斯消元解方程。
 
如果是一般的方程,高斯消元需要O(n*n)
 
但是这题的矩阵很特别
 
 
很快就可以消掉了。
用dp[i]表示i到n的期望。
dp[n]=0;
 
 
对于第i个方程,最多只有2*m+1个系数是不为0的。
 
 
 
 
 
 /* **********************************************
Author : kuangbin
Created Time: 2013/8/12 20:28:58
File Name : F:\2013ACM练习\比赛练习\2013杭州邀请赛重现\1004.cpp
*********************************************** */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;
const int MAXN = ;
double c[MAXN][];
double p[MAXN][];
double a[MAXN][];
double b[MAXN];
double dp[MAXN]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,m;
while( scanf("%d%d",&n,&m) == )
{
if(n == && m == )break;
for(int i = ;i <= n;i++)
for(int j = ;j <= m;j++)
scanf("%lf",&c[i][j]);
for(int i = ;i < n;i++)
{
double sum = ;
for(int j = ;j <= m;j++)
sum += c[i][j];
double s = ;
for(int j = ;j <= m && i-j >= ;j++)
{
p[i][m-j] = 0.3*c[i][j]/(+sum);
s += p[i][m-j];
}
for(int j = ;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] = -;
}
for(int i = ;i <= m+ && i <= n;i++)
a[][i] = p[][m+i-];
for(int i = ;i < n;i++)
{
int end = min(i+m,n);
int start = max(,i-m); for(int j = start;j < i;j++)
if(fabs(p[i][m+j-i]) > 1e-)
{
double t = p[i][m+j-i]/a[j][];
for(int k = ; k <= m+ && j+k- <= n ;k++)
{
p[i][m+j-i+k-] -= t*a[j][k];
}
b[i] -= t*b[j];
}
for(int j = ;j <= end-i+;j++)
a[i][j] = p[i][m+j-]; }
dp[n] = ;
for(int i = n-;i >= ;i--)
{
for(int j = ;j <= m+ && i+j- <= n;j++)
b[i] -= dp[i+j-] * a[i][j];
dp[i] = b[i]/a[i][];
}
printf("%.2f\n",dp[]);
}
return ;
}
 
 
 
 
 
 
 
 
 

HDU 4579 Random Walk (解方程组)的更多相关文章

  1. hdu 4579 Random Walk 概率DP

    思路:由于m非常小,只有5.所以用dp[i]表示从位置i出发到达n的期望步数. 那么dp[n] = 0 dp[i] = sigma(dp[i + j] * p (i , i + j)) + 1 .   ...

  2. 【HDOJ】4579 Random Walk

    1. 题目描述一个人沿着一条长度为n个链行走,给出了每秒钟由i到j的概率($i,j \in [1,n]$).求从1开始走到n个时间的期望. 2. 基本思路显然是个DP.公式推导也相当容易.不妨设$dp ...

  3. HDU 4487 Maximum Random Walk

    Maximum Random Walk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  4. 从Random Walk谈到Bacterial foraging optimization algorithm(BFOA),再谈到Ramdom Walk Graph Segmentation图分割算法

    1. 从细菌的趋化性谈起 0x1:物质化学浓度梯度 类似于概率分布中概率密度的概念.在溶液中存在不同的浓度区域. 如放一颗糖在水盆里,糖慢慢溶于水,糖附近的水含糖量比远离糖的水含糖量要高,也就是糖附近 ...

  5. matlab 解方程组

    1.解方程 最近有多人问如何用matlab解方程组的问题,其实在matlab中解方程组还是很方便的,例如,对于代数方程组Ax=b(A为系数矩阵,非奇异)的求解,MATLAB中有两种方法:(1)x=in ...

  6. 加入商品分类信息,考虑用户所处阶段的 图模型 推荐算法 Rws(random walk with stage)

    场景: 一个新妈妈给刚出生的宝宝买用品,随着宝宝的长大,不同的阶段需要不同的物品. 这个场景中涉及到考虑用户所处阶段,给用户推荐物品的问题. 如果使用用户协同过滤,则需要根据购买记录,找到与用户处于同 ...

  7. 醉汉随机行走/随机漫步问题(Random Walk Randomized Algorithm Python)

    世界上有些问题看似是随机的(stochastic),没有规律可循,但很可能是人类还未发现和掌握这类事件的规律,所以说它们是随机发生的. 随机漫步(Random  Walk)是一种解决随机问题的方法,它 ...

  8. 用列主元消去法分别解方程组Ax=b,用MATLAB程序实现(最有效版)

    数值分析里面经常会涉及到用MATLAB程序实现用列主元消去法分别解方程组Ax=b 具体的方法和代码以如下方程(3x3矩阵)为例进行说明: 用列主元消去法分别解方程组Ax=b,用MATLAB程序实现: ...

  9. Python编程:从入门到实践 - matplotlib篇 - Random Walk

    随机漫步 # random_walk.py 随机漫步 from random import choice class RandomWalk(): """一个生成随机漫步数 ...

随机推荐

  1. selenium只打开一个浏览器窗口

    from selenium.webdriver import Remote from selenium.webdriver.chrome import options from selenium.co ...

  2. java在图片上写字

  3. count(*)与count(1)、count('xxx')等在使用语法方面的区别

    语法方面: 区别就是:没有区别!!! “*”号是通配符: “*”号是通配符 “*”号是通配符 使用"*"号和使用其他数字和任意非字段字符在使用方面没有任何语法错误; 至于效率方面是 ...

  4. u-boot启动第二阶段以及界面命令分析

    u-boot第一阶段完成了一些平台相关的硬件的配置,第一阶段所做的事情也是为第二阶段的准备,我们知道在第一阶段最后时搭建好C运行环境,之后调用了start_armboot(),那么很显然第二阶段从st ...

  5. [ python ] 练习作业 - 1

    1,写代码,有如下列表,按照要求实现每一个功能 li = ["alex","wusir","eric","rain",& ...

  6. beego学习笔记(4):开发文档阅读(4)

    beego.Run() 这个运行后, 1)解析配置文件,也就是conf目录下的. 2)判断是否开启session,并作相应的工作 3)执行用户的hookfunc. 4)是否编译模板 5)是否开启文档功 ...

  7. MySQL5.7 centos7.2 yum 安装

    1.配置YUM源 在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo/yum/  # 下载mysql源安装包 shell> wge ...

  8. 原生js粘贴复制【源码】

    <html> <head> <meta charset="UTF-8"> <title>9行代码实现复制内容至剪切板</tit ...

  9. webstorm自动压缩js、css、html【工具篇】

    *注意:自动压缩的文件只能在同级目录下,不能指定文件夹,强制了文件自动保存,设置的手动保存将失效. 插件下载地址:点击这里下载 密码:e6bk 使用方法: 1.css&js 分别添加这两个,c ...

  10. Python全栈开发之7、模块和几种常见模块以及format知识补充

    一.模块的分类 Python流行的一个原因就是因为它的第三方模块数量巨大,我们编写代码不必从零开始重新造轮子,许多要用的功能都已经写好封装成库了,我们只要直接调用即可,模块分为内建模块.自定义的模块. ...