链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2191
思路:多重背包模板题
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <math.h> 5 #include <algorithm> 6 using namespace std; 7 int money,type; 8 int price[1005],weigh[1005],num[1005],dp[1005]; 9 int max(int a,int b) 10 { 11 return a>b?a:b; 12 } 13 void CompletePack(int price,int weigh) 14 { 15 for(int i=price;i<=money;i++) 16 dp[i]=max(dp[i],dp[i-price]+weigh); 17 } 18 void ZeroOnePack(int price,int weigh) 19 { 20 for(int i=money;i>=price;i--) 21 dp[i]=max(dp[i],dp[i-price]+weigh); 22 } 23 void MultiplePack(int price,int weigh,int num) 24 { 25 if(price*num>=money) 26 CompletePack(price,weigh); 27 int k=1; 28 while(k<num) 29 { 30 ZeroOnePack(k*price,k*weigh); 31 num-=k; 32 k>>1; 33 } 34 ZeroOnePack(num*price,num*weigh); 35 } 36 int main() 37 { 38 int t; 39 scanf("%d",&t); 40 while(t--) 41 { 42 scanf("%d %d",&money,&type); 43 memset(dp,0,sizeof(dp)); 44 for(int i=1;i<=type;i++) 45 { 46 scanf("%d %d %d",&price[i],&weigh[i],&num[i]); 47 MultiplePack(price[i],weigh[i],num[i]); 48 } 49 printf("%d\n",dp[money]); 50 } 51 return 0; 52 }