Pack-two-dimensional backpack

Source: Internet
Author: User

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

 

 

Output (pack. out ):

50

 

 

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.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.