Dynamic Planning (5)--01 knapsack problem (NYOJ289 Apple)

Source: Internet
Author: User

Apple
Describe

CTest has n apples to put into a backpack with a capacity of V. Give the size and price of the first Apple, and find out the total price of the apples that can be put into the backpack.

Input
There are several sets of test data, the first behavior of each group of test data 2 positive integers, respectively, the number of apples is n and the capacity of the backpack V,n, v at the same time to end the test at 0 o'clock, not output. The next n lines, 2 positive integers per line, are separated by a space, representing the size C and the price W of the apples, respectively. The range of all input numbers is greater than or equal to 0, less than or equal to 1000.
Output
output An integer for each set of test data, representing the total value of the apple that can be put into the backpack.
Sample input
3 31 12 13 10 0
Sample output
2
Ideas:
is a typical 01 backpack, using DP[I][J] to represent the maximum value from the first Apple to the nth Apple can be loaded into the backpack capacity of J; Dp[i][j]=dp[i+1][j] said that the first Apple is not loaded into the backpack (the first Apple is larger than the size of the backpack, can not fit); dp[i ][j]=max (Dp[i+1][j],dp[i+1][j-tiji[i]]+jiazhi[i]);(The volume of the first Apple is not larger than the backpack capacity, then see the apple into the backpack after the maximum value Dp[i+1][j-tiji[i]]+jiazhi [i]) and the maximum value when not in the backpack dp[i+1][j] which is the size of whichever value.
The AC code is as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> using namespace std; int dp[1005][1005]; int tiji[1005]; int jiazhi[1005]; int main() {     int n,v;     while(scanf("%d%d",&n,&v))     {         if(n==0&&v==0)             break;         memset(tiji,0,sizeof(tiji));         memset(jiazhi,0,sizeof(jiazhi));         for(int i=1;i<=n;i++)         {             scanf("%d",&tiji[i]);             scanf("%d",&jiazhi[i]);                }         memset(dp,0,sizeof(dp));         int jiaoxiao=min(v,tiji[n]-1);         for(int j=0;j<=jiaoxiao;j++)         {             dp[n][j]=0;//如果背包容量小于n的体积,那么就只装n苹果而言肯定是装不下。                }         for(int j=tiji[n];j<=v;j++)         {             dp[n][j]=jiazhi[n];                }         for(int i=n-1;i>1;i--)         {              jiaoxiao=min(tiji[i]-1,v);              for(int j=0;j<=jiaoxiao;j++)              {                 dp[i][j]=dp[i+1][j];//第i个苹果未装入                     }              for(int j=tiji[i];j<=v;j++)              {                  dp[i][j]=max(dp[i+1][j],dp[i+1][j-tiji[i]]+jiazhi[i]);                     }         }         dp[1][v]=dp[2][v];         if(tiji[1]<=v)         {             dp[1][v]=max(dp[2][v],dp[2][v-tiji[1]]+jiazhi[1]);                      }         printf("%d\n",dp[1][v]);     }     system("pause");     return 0; }

  

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.