Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2955
Because the probability of being captured is a real number, while in the knapsack problem, the number of lower-level indicators must be an integer, so the total amount of all banks is used as the subscript here, the escape rate (1-pj) as the Dp value
During initialization, DP [0] = 1 (the escape rate is 1); the others are 0, and you can follow the 0/1 backpack.
ACCode:
/* Hdoj2955 Author: Chen jiairun 2013-04-19 */# include <stdio. h> # include <string. h> # define max (A, B) (A> B? A: B) Double PJ [105]; // The escape rate of each bank int MJ [105]; // The amount of each bank double DP [10005]; int sum; // total amount of all banks void zeroonepack (double value, int weight) {int I; for (I = sum; I> = weight; I --) {DP [I] = max (DP [I], DP [I-weight] * value) ;}} int main () {int time, N, I; Double P; // freopen ("1.txt"," r ", stdin); scanf (" % d ", & time); While (Time --) {scanf (" % lf % d ", & P, & N); sum = 0; for (I = 1; I <= N; I ++) {scanf ("% d % lf ", & MJ [I], & PJ [I]); PJ [I] = 1-pj [I]; sum + = MJ [I];} // initialize memset (DP, 0, sizeof (DP); DP [0] = 1; // 0/1 backpack for (I = 1; I <= N; I ++) zeroonepack (PJ [I], MJ [I]); // find the maximum harvest that satisfies the escape probability (I = sum; I> = 0; I --) {If (DP [I]> 1-p) {printf ("% d \ n", I); break ;}} return 0 ;}