标准的多重背包应用,只要 看了 背包九讲应该就会做了的题目;
用结构体做比较省事;
这题比较蛋疼的是 中间把 数量打成重量,找了半天也找不出来,又看了几遍PO3还是没找出来...抱着奇怪的心里,搜了一下.我去...
唉..以后要好好注意写代码的习惯了...
1 #include<iostream> 2 #define maxn 105 3 #define max(a,b) (a>b?a:b) 4 int dp[maxn]; 5 int n,m,t; 6 struct node 7 { 8 int amount,weight,price; 9 }game[maxn]; 10 void zeroOnePack(int cost,int weight) 11 { 12 for(int i=n;i>=cost;i--) 13 dp[i]=max(dp[i],dp[i-cost]+weight); 14 } 15 void completePack(int cost ,int weight) 16 { 17 for(int i=cost;i<=n;i++) 18 dp[i]=max(dp[i],dp[i-cost]+weight); 19 } 20 void multiPack(int cost,int weight,int amount) 21 { 22 23 if(cost * amount >= n) 24 completePack(cost,weight); 25 else 26 { 27 int k=1; 28 while( k< amount) 29 { 30 zeroOnePack(k*cost,k*weight); 31 amount-=k; 32 k*=2; 33 } 34 zeroOnePack(amount*cost,amount*weight); 35 } 36 } 37 38 int main() 39 { 40 //freopen("2191.txt","r",stdin); 41 int i,j; 42 while(~scanf("%d",&t)) 43 { 44 while(t--) 45 { 46 memset(dp,0,sizeof(dp)); 47 memset(game,0,sizeof(game)); 48 scanf("%d %d" ,&n,&m); 49 for(i=0;i<m;i++) 50 scanf("%d %d %d",&game[i].price,&game[i].weight,&game[i].amount); 51 for(i=0;i<m;i++) 52 multiPack(game[i].price,game[i].weight,game[i].amount); 53 printf("%d ",dp[n]); 54 } 55 } 56 return 0; 57 }