01 knapsack Problem Small Change--combine probability __ dynamic programming

Source: Internet
Author: User
a-robberies

The aspiring Roy the robber has seen a lot of American movies, and knows the bad guys usually the gets Often because they become too greedy. He has decided to work in the lucrative business of bank robbery only for a short while, before retiring to a comfortable Job at a university.

For a few months now, Roy has been assessing the security of various banks and the amount of cash they. He wants to make a calculated risk, and grab as much as possible.


His mother, Ola, has decided upon a tolerable probability of getting caught. She feels that it safe enough if the banks he robs together give a probability less than this. 

Input
The ' a ' of input gives T, the number of cases. For each scenario, the the ' the ' of input gives a floating point number P, the probability Roy needs to is below, and an Integer N, the number of banks he has plans for. Then follow N lines, where line J gives an integer Mj and a floating point number Pj.
Bank J contains Mj millions, and the probability of getting caught from robbing it are Pj.
Output
For the all test case, output a-line with the maximum number of millions him can expect to get while the probability of gettin G caught is less than the limit set.

Notes and Constraints
0 < T <=
0.0 <= P <= 1.0
0 < N <=
0 < Mj <=
0.0 <= Pj <= 1.0
A Bank goes bankrupt if it is robbed, and your may assume this all probabilities are As the police have very low funds.

Sample Input

3
0.04 3
1 0.02
2 0.03
3 0.05
0.06 3
2 0.03
2 0.03
3 0.05
0.10 3 1
0.03
2 0.
3 0.05

Sample Output

2
4
6

The lad robbed the bank, the mother set the index, from the legal point of view ... Well back to the point, the main idea of this topic is dynamic planning, but will weight (volumn) changed to be police caught probability, as long as not more than the probability of mother set up, and then rob the money is actually value, then the equation is Dp[j]=max (dp[j), Dp[j-m[i]]*p[i], there is a probability problem, is high school knowledge, is the probability of being caught is P1,P2,P3,.... PN, we should consider from the negative thinking, consider 1-pi, that is not caught probability, with mutually exclusive event to deal with. This will not be less. Think, with the increase in the number of trees in the investment bank, the probability of being caught is gradually increasing, such a thought, no problem, this is the attention point, there is open dp[] array is to open to 10010, the size, because the bank's maximum amount is 100 *100; this is similar to the one-dimensional 01 knapsack problem, With the backpack capacity as a DP array subscript, in fact, use value to do subscript can also.
The code is as follows:

#include <iostream> #include <cstdlib> #include <cmath> #include <cstdio> #include <cstring&
Gt
#include <algorithm> #define INF 0x3f3f3f3f using namespace std;
Double dp[10010];
Double p[105];
int m[105];
    int main () {int t;
    cin>>t;
        while (t--) {memset (p,0,sizeof (p));
        memset (M,0,sizeof (m));
        Memset (Dp,0,sizeof (DP));
        Double Plimit;
        int I, J, N, Money=0;
        cin>>plimit>>n;
        for (i=1;i<=n;i++) cin>>m[i]>>p[i];
        for (i=1;i<=n;i++) money+=m[i];
        for (i=1;i<=n;i++) p[i]=1-p[i];

        Dp[0]=1;


            for (i=1;i<=n;i++) {for (j=money;j>=m[i];j--) {Dp[j]=max (dp[j],dp[j-m[i]]*p[i]); } for (j=money;j>=0;j--) {if (dp[j]>1-plimit) {COUT&LT;&LT;J&L
                t;<endl;
            Break }}} RetuRN 0;
 }

Today the bearing of the bicycle head is broken, can only go straight line, I drop a god ah ... Effort always pays, go back to sleepy high ....

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.