uva 12723 概率dp

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 ;
}
上一篇:基于jq的表单填充


下一篇:C++继承和多态