This is a simple 01 backpack, but the knapsack problem is fairly simple, is the front of the details of the time to pay attention to, test instructions roughly said three restrictions bar
1. Only A, B, c three types of invoices can be reimbursed, the other will not be reimbursed
2. No more than 600 reimbursement for item items
3. The total amount of each invoice does not exceed 1000
With these three, there is one to be careful is the amount of reimbursement can be floating point number, so here is a small trick, is to multiply it by 100, and then divided by 100, why multiply 100, because the last request to retain two decimal places
The code is as follows:
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 6 intdp[3000050];//Invoice Count 30* per total 1000* magnification = 30000007 intMain ()8 {9 Charch;Ten Doublex, y; One intSum, A, B, C, money[ *], t; A intN, K; - while(~SCANF ("%lf%d", &x, &n) &&N) - { thesum = (int) (X * -);//enlarge the capacity of the backpack to an integer (this is the maximum amount of reimbursement) -Memset (DP,0,sizeof(DP)); -memset (Money,0,sizeof(Money)); - intLen =0;//reimbursable list length, that is, the last n + for(inti =0; I < n; i++) - { +scanf"%d", &k); AA = b = c =0;//separate to save type A, type B, type C at BOOLFlag =true; - while(k--) - { -scanf"%C:%LF", &ch, &y); -T = (int) (Y * -); - if(ch = ='A'&& A + t <=60000) inA + =T; - Else if(ch = ='B'&& B + t <=60000) toB + =T; + Else if(ch = ='C'&& C + t <=60000) -B + =T; the Else *Flag =false; $ Panax Notoginseng } - if(A + B + C <=100000&& a <=60000&& b <=60000&& C <=60000&&flag) themoney[len++] = a + B + C;//if the above three conditions are met + } A for(inti =0; i < Len; i++)//01 Backpack Core Code the { + for(intj = sum; J >= Money[i]; j--) - if(Dp[j] < Dp[j-money[i]] +Money[i]) $DP[J] = Dp[j-money[i]] +Money[i]; $ } -printf"%.2f\n", (dp[sum]/100.0)); - } the return 0; -}
HDU-1864 Maximum reimbursement (01 backpack)