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. RAID总结

    RAID-0: 这种模式若使用相同型号容量的磁盘来组成效果最佳.这种模式RAID会先将磁盘切出等量的区块chunk,当文件要存入RAID时先按照chunk的大小切割好,再依次存放到各个磁盘中去,由于磁 ...

  2. Codeforces Round #441 (Div. 2)

    Codeforces Round #441 (Div. 2) A. Trip For Meal 题目描述:给出\(3\)个点,以及任意两个点之间的距离,求从\(1\)个点出发,再走\(n-1\)个点的 ...

  3. Python 正则表达式、re模块

    一.正则表达式 对字符串的操作的需求几乎无处不在,比如网站注册时输入的手机号.邮箱判断是否合法.虽然可以使用python中的字符串内置函数,但是操作起来非常麻烦,代码冗余不利于重复使用. 正则表达式是 ...

  4. Sublime Text2使用规则

    Sublime Text是我发现的有一好用的编辑器,它不单单只支持 python ,几乎支持目前主流的语言,快捷键丰富,可以极大的提高代码开发效率.Sublime Text 网址:http://www ...

  5. selenium grid应用1-多浏览器执行用例

    driver =webdriver.Remote(command_executor=’http://127.0.0.1:4444/wd/hub’, desired_capabilities=Desir ...

  6. [转载]FFmpeg完美入门[2] - FFmpeg参数说明

     1 通用选项 -L license -h 帮助 -fromats 显示可用的格式,编解码的,协议的. -f fmt 强迫采用格式fmt -i filename 输入文件 -y 覆盖输出文件 -t d ...

  7. C/C++——static修饰符

    1. static变量 static 用来说明静态变量.如果是在函数外面定义的,那么其效果和全局变量类似,但是,static定义的变量只能在当前c程序文件中使用,在另一个c代码里面,即使使用exter ...

  8. ansible command模块将返回值写入变量

    ansible 中command模块支持 register参数将远程命令执行的输出结果存储在变量中,后续可以在when中对该变量进行检索确定下一步任务. --- - name: cat /etc/re ...

  9. system()函数

    windows下system () 函数详解 windows操作系统下system () 函数详解(主要是在C语言中的应用) 函数名: system   功 能: 发出一个DOS命令   用 法: i ...

  10. 使用coding云作为git远程库

    1.在命令行中创建GIT仓库 mkdir DriveAssistant cd DriveAssistant git init echo "# DriveAssistant" > ...