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)