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. linux下使用privoxy将socks转为http代理

    此博客不在更新,我的博客新地址:www.liuquanhao.com ----------------------------------------------------------------- ...

  2. python RSA加密解密及模拟登录cnblog

    1.公开密钥加密 又称非对称加密,需要一对密钥,一个是私人密钥,另一个则是公开密钥.公钥加密的只能私钥解密,用于加密客户上传数据.私钥加密的数据,公钥可以解密,主要用于数字签名.详细介绍可参见维基百科 ...

  3. PSQueue队列操作

    队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈(FILO,First In Last Out,先进后出)属于线性表一样,队 ...

  4. golang-goroutine和channel

    goroutine 在go语言中,每一个并发的执行单元叫做一个goroutine 这里说到并发,所以先解释一下并发和并行的概念: 并发:逻辑上具备同时处理多个任务的能力 并行:物理上在同一时刻执行多个 ...

  5. hdu 5895(矩阵快速幂+欧拉函数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5895 f(n)=f(n-2)+2*f(n-1) f(n)*f(n-1)=f(n-2)*f(n-1)+2 ...

  6. Redis学习-redis概述

    最近刚刚接触了redis技术,对此有一些了解,这是简单做一点总结. Redis简介 首先,简单了解一下NoSQL(Not only sql),不要错误的理解为:没有SQL,而是不仅仅是SQL.NoSQ ...

  7. 解决 VUE 微信 IOS 路由跳转问题

    watch: { "$route"(){ if (/iPhone|mac|iPod|iPad/i.test(navigator.userAgent)) { location.hre ...

  8. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) G - Xor-matic Number of the Graph 线性基好题

    G - Xor-matic Number of the Graph 上一道题的加强版本,对于每个联通块需要按位算贡献. #include<bits/stdc++.h> #define LL ...

  9. Oracle表空间不足ORA-01654

    v在往数据表里插入数据时,出现了ORA-01654: 索引 SSERVICE.IX_MSI_WDR_INPUT_1 无法通过 1024 (在表空间 USERD 中) 扩展的错误信息,原来是数据量太大, ...

  10. 洛谷P2147 [SDOI2008] 洞穴勘探 [LCT]

    题目传送门 洞穴勘探 题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道 ...