HDU 2955 Robberies "01 backpack"

Source: Internet
Author: User

Problem-solving ideas: Give a critical probability, in the condition of not exceeding this probability, thief can steal how much money. Because there are only two options for stealing and not stealing for every bank, it is a 01 knapsack problem.

Here is a small conversion, that is, f[v] represents the amount of money in the package is V, the thief is not arrested the probability, so we are in use

for (i=1;i<=n;i++)

{

for (v=vol;v>=0;v--)

F[v]=max (f[v],f[v-c[i]]* (1-p[i));
}

, in the process of finding the maximum probability of not being caught, recording the amount of money in the package in the process and the corresponding probability at this time, so that only a cycle to determine the probability is greater than the threshold of the time to jump out of the loop, you get the money stolen

The capacity of the package is the total amount of money given by the N banks (that is, the amount of money that can be stolen regardless of the critical probability given), and the consumption of each item is the bank's money. The value of each item is (1-p[i]) (that is, the probability that the bank will not be caught)

Reflection: A shameful look at the puzzle, because the constant conversion is not the past, the probability because it is floating point type do not know how to convert, and then the capacity of the package is given to all the bank of the sum of money, also did not think.

Robberies

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) total submission (s): 13067 Accepted Submission (s): 4834

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 <= 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 that all probabilities be independent as the police has very Low funds. Sample Input30.04 31 0.022 0.033 0.050.06 32 0.032 0.033 0.050.10 31 0.032 0.023 0.05
#include <stdio.h> #include <string.h>int c[105];d ouble p[105], f[10010];d ouble max (double a,double b) {if (a >B) return A;elsereturn B;} int main () {int ncase,n,i,v,vol;double m;scanf ("%d", &ncase), while (ncase--) {vol=0;scanf ("%lf%d", &m,&n); for (i=1;i<=n;i++) {scanf ("%d%lf", &c[i],&p[i]);p [i]=1-p[i];vol+=c[i];} memset (F,0,sizeof (f)), F[0]=1;for (i=1;i<=n;i++) {for (v=vol;v>=0;v--) {F[v]=max (f[v],f[v-c[i]]*p[i]);p rintf ( "f[%d]=%lf\n", V,f[v]);}    } for (i=vol;i>=1;i--) {if (f[i]>=1-m) break;} printf ("%d\n", I);}}

  

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.