Probability DP hdu 3366

Meaning of the questions: a man trapped in a castle, before it n the road, he had m ​​million, select every road has a probability by p, q probability encounter soldiers, 1-pq probability impassable roads; in case soldiers then need to hand over a million, if not enough money, were killed, and asked how much the probability of the optimal situation can be managed to escape

The idea is first processed at the data, we want the best certainly without being killed out sooner the better, as far as possible not to touch it requires soldiers, so it should first sort the data in accordance with p / q, it can guarantee the maximum probability of death, then through equations have solutions to the probability dp

dp [i] [j] represents the way there i j millions of cases

Where is the i-th road escape dp [i] [j] * pi, the i-th road and without encountered soldiers killed can deduce where dp [i + 1] [j-1] of qi * dp [i] [j], where the i-th road barrier is folded dp [i] [j] * (1-pi-qi)

Note: Because the probability is that every time + =, so remember to clear the array dp
----------------
Disclaimer: This article is the original article CSDN bloggers "WShuo97", following CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/kuhuaishuxia/article/details/52042586

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=1000+5;
 6 struct passage
 7 {
 8     double p,q;
 9     bool operator <(const passage &r)
10     {
11         return p/q>r.p/r.q;
12     }
13 } a[maxn];
14 double dp[maxn][maxn];
15 int main()
16 {
17     int t;
18     scanf("%d",&t);
19     for(int T=1; T<=t; T++){
20         int n,m;
21         scanf("%d%d",&n,&m);
22         for(int i=1; i<=n; i++){
23             scanf("%lf%lf",&a[i].p,&a[i].q);
24         }
25         sort(a+1,a+1+n);
26         memset(dp,0,sizeof(dp));
27         dp[1][m]=1.0;
28         double ans=0;
29         for(int i=1;i<=n;i++)
30         for(int j=m;j>=0;j--){
31             dp[i+1][j-1]+=a[i].q*dp[i][j];
32             dp[i+1][j]+=dp[i][j]*(1-a[i].p-a[i].q);
33             ans+=dp[i][j]*a[i].p;
34         }
35         printf("Case %d: %.5lf\n",T,ans);
36     }
37     return 0;
38 }

 

Guess you like

Origin www.cnblogs.com/pangbi/p/11574629.html