Dudu is a very starving possum. He currently stands in the first shelf of a fridge. This fridge is
composed of N shelves, and each shelf has a number Qi (1 ≤ i ≤ N) of food. The top shelf, where
Dudu is, is identified by the number 1, and the lowest is identified by number N. Dudu doesn’t eat
more than one food in the same shelf, because he doesn’t want to get noticed. Furthermore, Dudu is
very fat and cannot climb the wall of the fridge to a shelf above — nobody knows how did he end up
in the first shelf. Dudu is also afraid of height, so he is only able to climb down at most K shelves at
a time (if he is at shelf i, he is only able to reach shelves i + 1, i + 2, . . . , i + K). There is a chance
Pj that he chooses to get down from a shelf i to a shelf i + j (1 ≤ j ≤ K). If he tries to go down a
number of shelves that makes him get past the lowest shelf, he gets out of the fridge — he will always
get out of the fridge eventually, because someone left the door open. Each food of shelf i has a number
of calories Ci,j that is absorbed by Dudu in case he eats it, and a probability Xi,j that it is chosen by
Dudu, for j from 1 to Qi
. Dudu starts his journey at shelf 1 and, when he is in a shelf, he will always
choose a food to eat and then will go to another shelf. What is the expected number of calories that
Dudu will absorb by the time he gets out of the fridge?
Input
The first line contains T (T ≤ 100) — the number of test cases, after this line T test cases follows.
The first line of a test case contains two integers, N and K (1 ≤ N ≤ 500; 1 ≤ K ≤ 10) — the
number of shelves in the fridge and the maximum number of shelves Dudu can climb down at a time,
correspondingly. The second line of a test case contains K real numbers Pj , where Pj is the probability
that Dudu goes down j shelves, for j from 1 to K (0 ≤ Pj ≤ 1; ∑K
j=1 Pj = 1). Each of the next N
lines of a test case describes a shelf (from the shelf 1 to shelf N). Each line starts with a integer Qi
(1 ≤ Qi ≤ 20), which is the amount of food existent is in this shelf. Qi pair follows, each pair containing
2 real numbers Ci,j and Xi,j (0 ≤ Ci,j ≤ 100; 0 ≤ Xi,j ≤ 1; ∑Qi
j=1 Xi,j = 1).
Output
For each test case print a line containing ‘Case #X: Y ’, where X is the case number, starting at 1,
and Y is the expected number of calories that Dudu will absorb by the time he gets out of the fridge.
Y should be rounded up to 6 digits after the decimal point.
Sample Input
2
2 1
1.0
2 50 0.5 100 0.5
2 10 0.5 20 0.5
5 2
0.3 0.7
5 10 0.2 20 0.3 5 0.1 25 0.35 2 0.05
2 20 0.4 40 0.6
1 4 1.0
3 30 0.8 3 0.1 4 0.1
10 1 0.1 2 0.1 3 0.1 4 0.1 5 0.1 6 0.1 7 0.1 8 0.1 9 0.1 10 0.1
Sample Output
Case #1: 90.000000
Case #2: 44.929950

题目大意:已知老鼠在第一层,只能往下走走到第n层结束。在第i层时可以走i+1、i+2、....、i+k,选择第i+j层的概率是Pj(1<=<=k)。每层有若干食物包括选它的概率跟它所含的卡路里,老鼠会选一种食物吃。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 100007 double ex[];
double dp[];
double pk[];
double C[][],X[][];
double sum[]; int main()
{
int t,cs = ,i,j,k;
int n,m,K,Q;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&K);
for(i=;i<=K;i++)
scanf("%lf",&pk[i]);
for(i=;i<=n;i++)
{
scanf("%d",&Q);
ex[i] = 0.0;
for(j=;j<=Q;j++)
{
scanf("%lf%lf",&C[i][j],&X[i][j]);
ex[i] += C[i][j]*X[i][j];
}
}
memset(dp,,sizeof(dp));
dp[] = ; //dp is probability
for(i=;i<=n;i++)
{
for(k=;k<=K;k++)
{
if(i-k >= )
dp[i] += dp[i-k]*pk[k];
}
}
double res = ;
for(i=;i<=n;i++)
res += dp[i]*ex[i];
printf("Case #%d: %.6lf\n",cs++,res);
}
return ;
}

uva 12723 概率dp的更多相关文章

  1. UVA 11427 (概率DP+期望)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35396 题目大意:每晚打游戏.每晚中,赢一局概率p,最多玩n局, ...

  2. [uva 11762]Race to 1[概率DP]

    引用自:http://hi.baidu.com/aekdycoin/item/be20a91bb6cc3213e3f986d3,有改动 题意: 已知D, 每次从[1,D] 内的所有素数中选择一个Ni, ...

  3. UVa 11427 Expect the Expected (数学期望 + 概率DP)

    题意:某个人每天晚上都玩游戏,如果第一次就䊨了就高兴的去睡觉了,否则就继续直到赢的局数的比例严格大于 p,并且他每局获胜的概率也是 p,但是你最玩 n 局,但是如果比例一直超不过 p 的话,你将不高兴 ...

  4. UVA 10900 So you want to be a 2n-aire? (概率dp)

    题意:玩家初始的金额为1:给出n,表示有n道题目:t表示说答对一道题目的概率在t到1之间均匀分布. 每次面对一道题,可以选择结束游戏,获得当前奖金:或者回答下一道问题,答对的话奖金翻倍,答错的话结束游 ...

  5. Substring UVA - 11468 AC自动机+概率DP

    题意: 给出一些字符和各自对应的选择概率,随机选择L次后得到一个长度为L的随机字符串S. 给出K个模板串,计算S不包含任何一个模板串的概率 dp[i][j]表示走到AC自动机 i 这个节点 还需要走 ...

  6. 概率dp专辑

    求概率 uva11021 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  7. DP专题之概率DP

    注意:在概率DP中求期望要逆着推,求概率要正着推 概率DP求期望: 链接: http://acm.hdu.edu.cn/showproblem.php?pid=4405 dp[ i ]表示从i点走到n ...

  8. Codeforces 28C [概率DP]

    /* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队 ...

  9. HDU 4405 Aeroplane chess (概率DP)

    题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i  这个位置到达 n ...

随机推荐

  1. mongodb 导入导出

    F:\Mongodb\bin>mongoexport.exe -h localhost:27017 -d proxy_db -c proxy_tb -o f:/p1.json 导出 -h 服务器 ...

  2. [BZOJ] 1127: [POI2008]KUP

    似曾相识的感觉 考虑另一个判断问题,给定一个k,问这个k是否可行 存在矩形和\(sum>2k\),则该矩阵不对判定做出贡献 存在矩形和\(sum\in [k,2k]\),则我们找到了一个解 于是 ...

  3. MySQL 自学笔记_Union(组合查询)

    1. Union查询简介 组合查询:有时在使用select语句进行数据查询时,想要将多个select语句在一个查询结果中输出,此时就需要使用Union关键字. Union的使用方法:用union将多个 ...

  4. Qt的由来和发展

    一.Qt的由来 Haavard Nord 和Eirik Chambe-Eng于1991年开始开发"Qt",1994年3月4日创立公司,早名为Quasar Technologies, ...

  5. Linux基础学习-使用vsftpd服务传输文件

    使用vsftpd服务传输文件 1 安装vsftpd [root@qdlinux ~]# yum install vsftpd Loaded plugins: product-id, search-di ...

  6. Python数据分析实战视频教程【小蚊子数据分析实战课程】

    点击了解更多Python课程>>> Python数据分析实战视频教程[小蚊子数据分析实战课程] [课程概述] Python数据分析实战' 适用人群:适合需提升竞争力.提升工作效率.喜 ...

  7. 微信JS-SDK 示例

    微信JS-SDK 示例 1.html部分 <!DOCTYPE html> <!-- saved from url=(0028){sh:$selfUrl} --> <htm ...

  8. Python基础——列表(list)

    创建列表(list) 通过[]来创建list结构,里面放任何类型都可以,没有长度限制. list1=[] type(list1) list1=[1,2,3,4] list1 list1=['] lis ...

  9. i2c drivers

    Linux设备驱动程序架构分析之一个I2C驱动实例   转载于:http://blog.csdn.net/liuhaoyutz 内核版本:3.10.1   编写一个I2C设备驱动程序的工作可分为两部分 ...

  10. The Best Path HDU - 5883 欧拉通路

    图(无向图或有向图)中恰好通过所有边一次且经过所有顶点的的通路成为欧拉通路,图中恰好通过所有边一次且经过所有顶点的回路称为欧拉回路,具有欧拉回路的图称为欧拉图,具有欧拉通路而无欧拉回路的图称为半欧拉图 ...