12723 probability DP

Source: Internet
Author: User

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
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
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
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)-
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 ining '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 shoshould 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

 

The mouse is known to be at the first layer and can only walk down to the end of the nth layer. In the I layer, I + 1, I + 2 ,.... and I + K. The probability of selecting the I + J layer is PJ (1 <== K ). Each layer has several foods, including the probability of choosing it and the calories it contains. Rats choose a food.

 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 probability37         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 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.