Hdoj 2955 robberies (0/1 backpack)

Source: Internet
Author: User

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 ;}

 

 

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.