Hdu2955_robberies [01 backpack]

Source: Internet
Author: User
Robberies

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/others) total submission (s): 12462 accepted submission (s): 4623
Problem description
The aspiring Roy the robber has seen a lot of American movies, and knows that the bad guys usually gets caught in the end, 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 varous banks and the amount of cash they hold. He wants to make a calculated risk, and grab as much money as possible.

His mother, OLA, has decided upon a tolerable probability of getting caught. She feels that he is safe enough if the banks he robs together give a probability less than this.
 
Input
The first line of input gives t, the number of instances. for each scenario, the first line of input gives a floating point number P, the probability Roy needs to be below, and an integer N, the number of banks he has plans. 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 is PJ.
 
Output
For each test case, output a line with the maximum number of millions he can perform CT to get while the probability of getting caught is less than the limit set.

Notes and constraints
Zero <t <= 100
0.0 <= P <= 1.0
0 <n <= 100
0 <MJ <= 100
0.0 & lt; = PJ & lt; = 1.0
A bank goes bankrupt if it is robbed, and you may assume that all probabilities are independent 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.02
3 0.05

Sample output
2
4

6


A robber wants to steal money from several banks. He wants to invest more money and try not to be caught. Known banks

The amount of money, the probability of being caught, and the maximum probability of being caught that the robbers can tolerate. How much can he steal?

Thought: the knapsack problem was originally intended to regard probability as a backpack, in which the maximum amount of money can be snatched.

But the problem lies in the probability. First, because the probability is a floating point number, it must be increased by 10 ^ n times for a backpack. Second, no

The probability of being caught is not a simple accumulation. Second, P = (1-p1) (1-p2) (1-p3) where p is the maximum probability of not being captured, P1, P2, P3

Probability of banks being arrested.

The second thought of taking the bank's money as a backpack, and taking the probability as the value. The total capacity is the total amount of money for all banks.

What is the maximum backpack capacity when the probability is reached?

DP [J] = max (DP [J], DP [J-bag [I]. v] * (1-bag [I]. p) (dp [J] indicates the money that can be snatched under the probability of being snatched );

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct bag{    int v;    double p;}Bag[10010];double dp[10010];int main(){    int T,N;    double p;    scanf("%d",&T);    while(T--)    {        scanf("%lf %d",&p,&N);        int sum = 0;        for(int i = 0; i < N; i++)        {            scanf("%d%lf",&Bag[i].v,&Bag[i].p);            sum += Bag[i].v;        }        memset(dp,0,sizeof(dp));        dp[0] = 1;        for(int i = 0; i < N; i++)        {            for(int j = sum; j >= Bag[i].v; j--)            {                dp[j] = max(dp[j],dp[j-Bag[i].v]*(1-Bag[i].p));            }        }        for(int i = sum; i >= 0; i--)        {            if(dp[i] > 1-p)            {                printf("%d\n",i);                break;            }        }    }    return 0;}



Hdu2955_robberies [01 backpack]

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.