HDU 2955 (01 backpack)

Source: Internet
Author: User

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 #defineSC (x) scanf ("%d",& (x))6 #definePF (x) printf ("%d\n", X)7 #defineCL (x, y) memset (x, y, sizeof (x))8 #defineMax (A, B) (a > B? a:b)9 using namespacestd;Ten Const intMAX = the; One structNode//Set as knapsack problem, structure body A { -     intp; -     DoubleW; the }bag[max]; - DoubleDp[max*max], P;//the probability of being caught - intm, T, I, J, N; - intMain () + { - SC (T); +      while(t--) A     { atm =0; -scanf"%lf%d", &p, &N); -          for(i=0; i<n; i++) -         { -scanf"%D%LF",&bag[i].p,&BAG[I].W); -M + =BAG[I].P; in         } -CL (DP,0); todp[0] =1; +          for(i =0; i < N; i++) -              for(j = m; j >= BAG[I].P; j--) theDP[J] = max (Dp[j], dp[j-bag[i].p]* (1-bag[i].w)); *          for(i = m; i>=0; i--) $         {Panax Notoginseng             if(Dp[i] >1-P) -             { the PF (i); +                  Break; A             } the         } +     } -     return 0; $}
View Code

Train of thought 1: Suppose altogether robbed J Yuan money, be caught probability for dp[j], this J Yuan money whether rob the first I all bank, assume the first bank has value[i][0] yuan, the probability of being caught is value[i][1], so dp[j] = min (dp[j), 1-(1-dp[ J-VALUE[0]]) * (1-value[i][1])). (Set value[][0], value[][1])

Idea 2: The most important thing is to find the dynamic transfer equation, you can think of all the money in the bank as a backpack capacity, each bank's money as a weight, the probability of not being caught as a value, then the transfer equation is: dp[J]=max (Dp[J], dp[j-bag[i].v]* (1-bag[i] . p));

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