多重背包
| Weight | Value | Amount |
A | 2 | 3 | 3 |
B | 1 | 2 | 2 |
C | 3 | 4 | 1 |
D | 2 | 2 | 2 |
问题:有n种物品,每种物品有amount[i]个,每个物品的重量为weight[i],每个物品的价值为value[i].现在有一个背包,他所能容纳的容量为total,问:当你面对多么有价值的物品时,你的背包所能带走的最大价值是多少?
| amoun | 0 | 1 | 2 | 3 | 4 | 5 |
A | 3 | 0 | 0 | 3 | 3 | 6 | 6 |
B | 2 | 0 | 2 | 4 | 3 | 6 | 6 |
C | 1 | 0 | 2 | 4 | 4 | 6 | 8 |
D | 2 | 0 | 2 | 4 | 4 | 6 | 8 |
|
|
|
|
|
|
|
|
| 0 | 1 | 2 | 3 | 4 | 5 |
A | 0 | 0 | 3 | 3 | 6 | 6 |
B | 0 | 2 | 4 | 6 | 8 | 10 |
C | 0 | 2 | 4 | 6 | 8 | 10 |
D | 0 | 2 | 4 | 6 | 8 | 10 |
#include <stdio.h>
#include <string.h>
int max(int a,int b){
if(a>b)
return a;
else
return b;
}
int main(){
int goods,i,weight[100],value[100],amount[100],rad[100],j,count[100],capacity;
printf("please imput the number of the goods:\n");
scanf("%d",&goods);
printf("please imput the weight and the value and the amount of the goods:\n");
for(i=1;i<=goods;i++){
scanf("%d%d%d",&weight[i],&value[i],&amount[i]);
}
scanf("%d",&capacity);
memset(rad,0,sizeof(rad));
for(j=1;j<=goods;j++){
for(i=0;i<=capacity;i++){
count[i]=0;
if(i==0){
rad[i]=0;
printf("%d ",rad[i]);
}
else if(i<weight[j]){
printf("%d ",rad[i]);
}
else{
if(count[i-weight[j]]<amount[j]){
if(rad[i-weight[j]]+value[j]>rad[i]){
rad[i]=rad[i-weight[j]]+value[j];
printf("%d ",rad[i]);
count[i]=count[i-weight[j]]+1;
}
else{
printf("%d ",rad[i]);
}
}
else{
printf("%d ",rad[i]);
}
}
}
printf("\n");
}
printf("%d\n",rad[capacity]);
return 0;
}