Poj 3624 01 backpack Problems

Source: Internet
Author: User

I haven't done any DP questions for a long time. I saw the puzzle question of FB, which is obviously a backpack problem, but I still can't write code. Therefore, the dedicated exercise code is king.

This topic is basically the 01 backpack, but it is still not written. It was just half a day after someone else's program was imitated. This article is your own study notes. If you think it is simple, please make a detour. Haha.

Start with the question:

Charm bracelet
Time limit:1000 ms   Memory limit:65536 K
Total submissions:12884   Accepted:5878

Description

Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she 'd like to fill it with the best charms possible fromN(1 ≤N≤ 3,402) Available charms. Each charmIIn the supplied list has a weightWi
(1 ≤Wi≤ 400), a 'inclurability 'factorDi(1 ≤Di≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no moreM(1 ≤M≤ 12,880 ).

Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.

Input

* Line 1: two space-separated integers:NAndM
* Lines 2 ..N+ 1: LineI+ 1 describes charmIWith two space-separated integers:WiAndDi

Output

* Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints

Sample Input

4 61 42 63 122 7

Sample output

23

 

Obviously 01.

First, analyze the sub-problem. When the I-th backpack is selected and the maximum weight is W, the maximum value is represented by matmaxvalue [I] [W.

When deciding on the I-th backpack, we have two options: Select one or not.

The premise of selection is that the current W is heavier than the backpack, and the value obtained after the selection is higher than that obtained after the selection.

 

So the formula matmaxvalue [I] [W] = max (matmaxvalue [I-1] [W], matmaxvalue [I-1] [w-w [I] + value [I]);

However, if this question is written as a two-dimensional array, there must be insufficient space. After observation, we found that, in fact, we decided to I, the sum of I-1, so only two Arrays can be used to complete this iteration process.

The Code is as follows:

#include <stdio.h>#include <string.h>int nJewery;int nWeightSum;struct Jewelry{int charms;int weights;};int maxValue1[12881], maxValue2[12881];Jewelry v[3410];int GetMaxValue(){int i,j;memset(maxValue1, 0, sizeof(maxValue1));memset(maxValue2, 0, sizeof(maxValue2));for( i = 0; i < nJewery; i++){for(j = 1; j <= nWeightSum; j++){if( j >= v[i].weights){int selectValue = maxValue1[j-v[i].weights] + v[i].charms; if(maxValue1[j] < selectValue ){maxValue2[j] = selectValue;}else{maxValue2[j] = maxValue1[j];}}else{maxValue2[j] = maxValue1[j];}}memcpy(maxValue1, maxValue2, sizeof(maxValue2));}return maxValue2[nWeightSum];}int main(){while(scanf("%d%d", &nJewery, &nWeightSum) != EOF){for(int i = 0; i < nJewery; i++){scanf("%d%d", &v[i].weights, &v[i].charms);}printf("%d\n",GetMaxValue());}return 0;}

However, the space for this question can be saved. Here two arrays are used, one representing the result of the I-1, and the other representing the final result of the current selection. In fact, we can see that when we traverse the current weight and J, the value that affects this decision must be smaller than J. In other words, when considering the I items, and the total weight is J, 0-j-1 has reached the optimal solution. Therefore, if we change the weight Traversal method from large to small, instead of being small to large, you can omit this part of space. For example, we traverse to the weight J, then J to sumweight save is to accommodate the optimal solution of I items, and 0 to J-1 is the optimal solution of I-1 objects. The core code is as follows: we only use a vdesirability array to understand the set. Saves half of the space while making the program look nice.

int GetMaxValue(){ int i,j; memset(vDesirability, 0, sizeof(vDesirability)); for( i = 0; i < nJewery; i++) {  for( j = nWeightSum; j > 0; j--)  {   if(j >= v[i].weights)   {    vDesirability[j] = std::max(vDesirability[j-v[i].weights] + v[i].charms, vDesirability[j]);   }  } } return vDesirability[nWeightSum];}

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.