[HDU 2955] robberies (Dynamic Planning)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2955

The question is to give you a probability P, and n Banks

I want to steal money now. I can steal m yuan in every bank, but there is a probability that P will be caught.

Ask how much money you can steal if the probability of being caught is lower than P.

 

At the beginning, I was still thinking that the probability of Bank A being captured is A, and that Bank B be arrested is B. Then, the probability of stealing a and B is a * B ..

--.. A * B is captured by both Bank A and Bank B ..

 

Therefore, the escape probability is used for calculation.

DP [I] [J] indicates the maximum probability that J yuan was stolen from the previous bank.

 

Code:

 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6  7 const int MAX_N = 110; 8 const double EPS = 1e-8; 9 double dp[MAX_N][MAX_N*MAX_N];10 int T,N,m[MAX_N];11 double P,p[MAX_N];12 13 int main(){14     scanf("%d",&T);15     while( T-- ){16         scanf("%lf%d",&P,&N);17         P = 1 - P;18         int summ = 0;19         for(int i=1;i<=N;i++){20             scanf("%d%lf",&m[i],&p[i]);21             p[i] = 1 - p[i];22             summ += m[i];23         }24         //for(int i=0;i<=summ;i++) dp[1][i] = 1;25         dp[0][0] = 1;26         for(int i=1;i<=N;i++){27             for(int j=0;j<=summ;j++){28                 if( j-m[i]>=0 ) dp[i][j] = max(dp[i-1][j],dp[i-1][j-m[i]]*p[i]);29                 else dp[i][j] = dp[i-1][j];30             }31         }32         int ans = 0;33         for(int i=0;i<=summ;i++){34 //            printf("%f\n",dp[N][i]);35             if( P-dp[N][i]<=EPS ){36 //                printf("dp[N][i]=%f < P=%f\n",dp[N][i],P);37                 ans = i;38             }39         }40 //        puts("");41         printf("%d\n",ans);42     }43     return 0;44 }45 46 /*47 348 0.04 349 1 0.0250 2 0.0351 3 0.0552 0.06 353 2 0.0354 2 0.0355 3 0.0556 0.10 357 1 0.0358 2 0.0259 3 0.0560 */

 

[HDU 2955] robberies (Dynamic Planning)

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.