Package [pack. PAS/pack. c/pack. cpp]
[Problem description] Now you get a lot of gifts. You need to put these gifts in the bag. You only have one bag containing up to v volume items, and you cannot put it all in. You cannot take anything that is so heavy. The maximum weight you can get is G. Now you know the perfect value, weight, and size of each item. Of course you want to maximize the total value of the perfect item in the bag. You have to plan it again.
[Input] the first line is V and G, indicating the maximum weight and volume. Row 2: N indicates that N gifts are received. Lines 3 to n + 2: 3 in each row. Ti vi gi indicates the perfect value, weight, and volume of each gift. [Output] The total number is output, indicating the maximum possible perfect value.
[Input and output sample]
Input (pack. In ): 6 5 4 10 2 2 20 3 2 40 4 3 30 3 3 |
|
|
The Code is as follows:
Code
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 int f[381][381],wm,vm,n,w[381],v[381],p[381];
5 FILE *in,*out;
6 int main(){
7 in=fopen("pack8.in","r");
8 out=fopen("pack.out","w");
9 fscanf(in,"%d%d%d",&wm,&vm,&n);
10 int i,j,k;
11 for(i=1;i<=n;i++)
12 fscanf(in,"%d%d%d",&p[i],&w[i],&v[i]);
13
14 memset(f,0,sizeof(f));
15 for(i=1;i<=n;i++)
16 for(j=wm;j>=w[i];j--)
17 for(k=vm;k>=v[i];k--)
18 if(f[j][k]<f[j-w[i]][k-v[i]]+p[i])f[j][k]=f[j-w[i]][k-v[i]]+p[i];
19
20
21 fprintf(out,"%d\n",f[wm][vm]);
22
23 fclose(in);
24 fclose(out);
25 return 0;
26 }
Because there is one more limit, the corresponding One-Dimension Data is written in the 0-1 backpack.