HDU 2955 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): 5181 Accepted Submission (s): 1958


Problem DescriptionThe Aspiring Roy the robber have seen a lot of American movies, and knows so 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.

Inputthe 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.

Outputfor each test case, output a line with the maximum number of millions he can expect to get while the probability of Getting 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 INPUT3 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 Output2 4 6

Sourceidi Open 2009

Recommendgaojie


Analysis: The bank's non-capture probability as the value of the goods, the bank's money as the weight of the goods, converted to 01 knapsack problem. Finally, according to the amount of money from the big to the small check, to find a maximum amount of money not to be caught.




AC Code:

#include <bits/stdc++.h>using namespace Std;const int maxn = 105;double DP[MAXN * maxn];int VALUE[MAXN];d ouble weigh T[maxn];int Nvalue, nkind;void zeroonepack (int cost, double weight) {for (int i=nvalue; i>=cost; i--) dp[i] = Max (Dp[i], dp[i-cost] * weight);}    int main () {#ifdef sxk freopen ("In.txt", "R", stdin);    #endif//sxk int T;    Double P;    scanf ("%d", &t);        while (t--) {scanf ("%lf%d", &p, &nkind);        Nvalue = 0;            for (int i=0; i<nkind; i++) {scanf ("%d%lf", &value[i], &weight[i]);            Weight[i] = 1-weight[i];        Nvalue + = Value[i];        } memset (DP, 0, sizeof (DP));        Dp[0] = 1;        for (int i=0; i<nkind; i++) Zeroonepack (Value[i], weight[i]);        int i;        for (I=nvalue; i>=0; i--) if (Dp[i] >= 1-p) break;    printf ("%d\n", I); } return 0;}





HDU 2955 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.