概率dp HDU 4405

Aeroplane chess

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

Submit Status Practice HDU
4405

Appoint description: 
System Crawler  (2014-10-18)

Description

Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal probability to face up and the numbers on the faces are
1,2,3,4,5,6). When Hzz is at grid i and the dice number is x, he will moves to grid i+x. Hzz finishes the game when i+x is equal to or greater than N.

There are also M flight lines on the chess map. The i-th flight line can help Hzz fly from grid Xi to Yi (0<Xi<Yi<=N) without throwing the dice. If there is another flight line from Yi, Hzz can take the flight line continuously. It is granted that there is
no two or more flight lines start from the same grid.

Please help Hzz calculate the expected dice throwing times to finish the game.

Input

There are multiple test cases.

Each test case contains several lines.

The first line contains two integers N(1≤N≤100000) and M(0≤M≤1000).

Then M lines follow, each line contains two integers Xi,Yi(1≤Xi<Yi≤N).

The input end with N=0, M=0.

Output

For each test case in the input, you should output a line indicating the expected dice throwing times. Output should be rounded to 4 digits after decimal point.

Sample Input

 2 0
8 3
2 4
4 5
7 8
0 0 

Sample Output

 1.1667
2.3441 

题意:Hzz在玩一种游戏,在N+1个格的图上,初始在0处,每次掷一枚骰子,骰子有6个标有1,2,3,4,5,6的面,每次前进最上面的那个面上所标的数步。有些地方有道具,到达之后它可以使用道具直接到达yi处,而不用掷骰子。求到达终点掷骰子的期望。

/*************************************************************************
    > File Name: t.cpp
    > Author: acvcla
    > Mail: [email protected]
    > Created Time: 2014年10月21日 星期二 21时33分55秒
 ************************************************************************/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<cstdlib>
#include<ctime>
#include<set>
#include<math.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
double dp[maxn];
int y[maxn],n,m;
int main(int argc, char const *argv[])
{
	while(~scanf("%d%d",&n,&m)){
		if(!n&&!m){
			return 0;
		}
		n++;
		memset(dp,0,sizeof dp);
		memset(y,0,sizeof(y));
		int x,to;
		for(int i=1;i<=m;i++){
			scanf("%d%d",&x,&to);
			y[x+1]=to+1;
		}
		for(int i=n-1;i>=1;i--){
			double t=0;
			for(int j=1;j<=6;j++)t+=dp[i+j]/6;
			if(y[i]){
				t=dp[y[i]]-1;
			}
			dp[i]=t+1;
		}
		printf("%.4f\n",dp[1]);
	}
	return 0;
}
时间: 2024-10-17 17:56:51

概率dp HDU 4405的相关文章

[概率dp] hdu 5001 Walk

题意:n个点(1~n),m条边,走k次,双向边,每次随机走一条路,起点也随机,问不走到各个点的概率是多少. 思路: 概率dp[i][j][k] 前i步 走到j 走不到k的概率. 那么状态转移就是 j能走到的点,传递所有dp[i][j][k]的值乘上概率. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #include"

[AC自动机+概率dp] hdu 3689 Infinite monkey theorem

题意: 给n个字母,和m次数. 然后输入n个字母出现的概率 然后再给一个目标串str 然后问m次中敲出目标串的概率是多少. 思路: AC自动机+概率dp的简单题. 首先建立trie图,然后就是状态转移了 dp版本: dp三重循环变量次数,节点数,和字母数 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #include"

概率dp HDU 3853

H - LOOPS Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3853 Appoint description:  System Crawler  (2014-10-22) Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wa

[概率dp] hdu 5378 Leader in Tree Land

题意: 给你一颗以1位根节点的树,我们定义对于每个子树,节点权值最大的权值记为这个子树的权值,为你将1~n放到这个树里 满足最大权值只有k个的组合数是多少. 思路: 我们可以知道以每个节点为子树,且根节点权值最大的概率是多少,不是的概率是多少. 那么其实问题就变成了 我们在n个物品里面,每个物品拿的概率是pi不拿的概率是1-pi 问你拿k个物品的概率是多少 然后最后乘n!就好了.中间计算运用逆元. 代码: #include"cstdlib" #include"cstring&

HDU 4405 Aeroplane chess (概率DP求期望)

题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点需要步数的期望 其中有m个跳跃a,b表示走到a点可以直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点可以到走到i+1,i+2,i+3,i+4,i+5,i+6 点且每个点的概率都为1/6 所以dp[i]=(dp[i+1]+dp[i+2]+dp[i+3]+dp[i+4]+dp[i+5]+dp[i+6])/6  + 1(步数加一). 而对于有跳跃的点直接为dp[a]=dp[b]; #include<stdio.h>

HDU 4405:Aeroplane chess(概率DP入门)

http://acm.split.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six f

[ACM] hdu 4405 Aeroplane chess (概率DP)

Aeroplane chess Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal probability to face up and the number

hdu 4405 概率dp 2012年金华亚洲网络赛--虽然水,但是是自己独立做的第一道概率dp

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4405 e[i]:当前在位置i还需要走的步数期望 受刘汝佳的AC自动机那个后缀链接写法的启发,我的x[i]通过逆序算出来连续有"flight line "的时候,能到达的最远距离, rep(i,0,m) { scanf("%d%d",&xx,&yy); x[xx]=yy; } for(int i=n;i>=0;i--) if(x[i]!=-1 &

hdu 4405 Aeroplane chess (概率DP)

Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1503    Accepted Submission(s): 1025 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids la