Blue Bridge Cup 01 backpack memory words Array

Source: Internet
Author: User

There are n items with a weight and value of Wi,vi respectively. Select items from these items that have a total weight of not more than W, and ask for the maximum value of the sum of all the values in the selection scheme

/**

* First items are recorded dynamically, and the total weight is less than J

* and keep the accepted records dynamically in the memory array

*/

#include <stdio.h>
#include <string.h>
int n,w;
int w[100],v[100];
Using a memory array
int dp[100][100];
int max (int n,int m) {
Return n>m?n:m;
}
Starting from Article I items, select the part with a total weight of less than J
int rec (int i,int j) {
If the current location is stored in the data, the data is returned directly
if (dp[i][j]>=0) return dp[i][j];
Record the current item weight
int res;
When the last item is selected, you can add a weight of 0
if (i==n) res=0;
If the current item weight is greater than the weight that can be added, the current item cannot be selected
else if (w[i]>j) Res=rec (I+1,J);
Else
Choose to select this item, or do not select the maximum weight of this item
Res=max (Rec (I+1,J), rec (i+1,j-w[i]) +v[i]);
Returns the maximum weight of the last item
Save data that has already been recorded
return dp[i][j]=res;
}
int main () {
while (scanf ("%d%d", &n,&w) ==2) {
Memset (Dp,-1,sizeof (DP));
for (int i=0;i<n;i++)
scanf ("%d%d", &w[i],&v[i]);
printf ("%d\n", rec (0,w));
}
return 0;
}

/************************************************ 2nd notation, recursive expression ************************************************** **/

#include <stdio.h>
#include <string.h>
int n,w;
int v[100],w[100];
DP for assistance Array
int dp[100][100];
int max (int n,int m) {
Return n>m?n:m;
}
VOID Rec () {
Choose from the last backpack
Use I to denote the backpack of the first I
for (int i=n-1;i>=0;i--)
J means the part with a total weight of less than J
for (int j=0;j<=w;j++) {
if (J<w[i]) dp[i][j]=dp[i+1][j];
else{
Dp[i][j]=max (Dp[i+1][j],dp[i+1][j-w[i]]+v[i]);
}
}
printf ("%d\n", Dp[0][w]);
}
int main () {
while (scanf ("%d%d", &n,&w) ==2) {
Memset (Dp,-1,sizeof (DP));
for (int i=0;i<n;i++)
scanf ("%d%d", &w[i],&v[i]);
Rec ();
}
return 0;
}

Blue Bridge Cup 01 backpack memory words Array

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.