ACM 01 knapsack problem

Source: Internet
Author: User

Description

The aspiring Roy the robber have 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 had been assessing the security of various 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, had decided upon a tolerable probability of getting caught. She feels that he's 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 cases. For each scenario, the first line 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's Pj.

Output

For each test case, output a line with the maximum number of millions he can expect to get while the probability of gettin G caught is less than the limit set.

Notes and Constraints
0 < T <= 100
0.0 <= P <= 1.0
0 < N <= 100
0 < Mj <= 100
0.0 <= Pj <= 1.0
A Bank goes bankrupt if it is robbed, and your may assume that all probabilities be independent as the police have very lo W funds.

Sample Input

30.04 31 0.022 0.033 0.050.06 32 0.032 0.033 0.050.10 31 0.032 0.023 0.05

Sample Output

246 Problem Solving Ideas: The main idea is that Roy wants to rob the bank, every bank has a certain amount of money and the probability of being caught, know that Roy was caught the maximum probability p, ask Roy to rob without being caught in the case of the most robbery. This is a knapsack problem, we just have to do a little conversion. Consider the sum of the money stored in each bank as a backpack capacity, and then the probability as the value of the request. Here is the probability of being caught, we turn him into a probability of not being caught, and then the sum of the here can be transformed into a product. Then use the 01 backpack template can be made out. Program code:
#include <cstdio> #include <iostream> #include <cstring>using namespace Std;int money[101], nkind, sum    ; float pro[101], Npro, Fine[11100];int main () {int cas,i, J;          scanf ("%d", &cas);//Case number while (CAS--) {sum = 0;              scanf ("%f%d", &npro, &nkind);//the highest probability number, and bank number for (i = 0; i < nkind; i + +) {          scanf ("%d%f", Money+i, pro+i);//bank money and the probability of being caught sum + = money[i];          } memset (fine, 0, sizeof (fine)); Fine[0] = 1;          The money in the backpack is 0 o'clock and is the safest, so the safety probability is 1 float p = 1-npro;//minimum security probability for (i = 0; i < nkind; i + +)//Bank number {for (j = sum; J >= Money[i]; J--)//if (Fine[j] < fine[j-money[i]]* (1-pro[i                  ])//Rob to J Dollar security probability for Fine[j] {fine[j] = fine[j-money[i]]* (1-pro[i]);             }} for (i = sum; I >= 0; i--) if (Fine[i] >= p)//The probability of security is greater than or equal to the lowest probability of being caught {printf ("%d\n", I);              break; }} return 0;}

  

ACM 01 knapsack problem

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.