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)。每层有若干食物包括选它的概率跟它所含的卡路里,老鼠会选一种食物吃。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 using namespace std; 7 #define N 100007 8 9 double ex[516]; 10 double dp[514]; 11 double pk[13]; 12 double C[504][25],X[504][24]; 13 double sum[13]; 14 15 int main() 16 { 17 int t,cs = 1,i,j,k; 18 int n,m,K,Q; 19 scanf("%d",&t); 20 while(t--) 21 { 22 scanf("%d%d",&n,&K); 23 for(i=1;i<=K;i++) 24 scanf("%lf",&pk[i]); 25 for(i=1;i<=n;i++) 26 { 27 scanf("%d",&Q); 28 ex[i] = 0.0; 29 for(j=1;j<=Q;j++) 30 { 31 scanf("%lf%lf",&C[i][j],&X[i][j]); 32 ex[i] += C[i][j]*X[i][j]; 33 } 34 } 35 memset(dp,0,sizeof(dp)); 36 dp[1] = 1; //dp is probability 37 for(i=2;i<=n;i++) 38 { 39 for(k=1;k<=K;k++) 40 { 41 if(i-k >= 1) 42 dp[i] += dp[i-k]*pk[k]; 43 } 44 } 45 double res = 0; 46 for(i=1;i<=n;i++) 47 res += dp[i]*ex[i]; 48 printf("Case #%d: %.6lf\n",cs++,res); 49 } 50 return 0; 51 }
原文:http://www.cnblogs.com/xiong-/p/3863940.html