Poj 1742 multiple backpack Problems

Source: Internet
Author: User

In the nine-read backpack, the author mentioned that the last basic problem with a backpack is the problem of multiple backpacks.

Among all the problems with backpacks, the problem is the same: There are several objects, P (WI, vi), each weight is represented by Wi, and the value obtained after selection is represented by VI. Then there is a total capacity. If the total capacity is not exceeded, the selected object will be the most valuable as possible.

01 in a backpack, only one object can be selected or not selected.

In the case of a full backpack, there are countless or numerous objects.

In multiple backpacks, the number of objects is limited, represented by P (WI, Vi, AI. You can select up to AI.

In "backpack 9 reading", the author gives a way of thinking that the problem of multiple backpacks should be divided into 01 backpacks. The principle of dismantling is to break down objects.

If we have 13 objects, how can we break them down into a group of integers so that their combination can represent any one of the 1-13 objects? The idea given by the author is to use the binary decomposition method, splits an integer into 1 2 4... and other factors.

But the last factor is learned, because we cannot make the sum of all the decomposed numbers greater than that of AI, so the last number is Ai-(1 + 2 +... 2 ^ (k-1) = ai-(2 ^ k-1)> 0

Therefore, the above solution is to break it into a base like 1 2 4 6. Then, we will understand the doubled object as a new object, and the 2x object as a new object .....

Complexity reduced to O (N * m * log ()).

However, this algorithm does not seem so beautiful. No aha.

The concept of building Instructors does not seem very easy to understand. I feel that the following methods are widely used on the Internet to convert from a full backpack to multiple backpacks.

When processing coins in Section I, we record the number of current coins required to obtain each monetary value. If this number meets the conditions (less than or equal to the given number ), we will select this coin, otherwise we will not.

With an array, we can limit the full backpack problem to implement a multi-backpack.

The Code is as follows:

#include <stdio.h>#include <string.h>#define COIN_NUM 105#define MONEY_SUM 100005int coin[COIN_NUM];int amount[COIN_NUM];int dp[MONEY_SUM];int used[MONEY_SUM];int nCoin, nSum;int GetNumOfPayments(){memset(dp, 0, sizeof(dp));dp[0] = 1;int i ,j;for(i = 0; i < nCoin; i++){memset(used, 0, sizeof(used));for( j = coin[i]; j <= nSum; j++){
// Because this problem only determines whether or not, you can use bool to implement if (DP [J-coin [I] &! DP [J] & used [J-coin [I] + 1 <= amount [I]) {DP [J] = 1; used [J] = used [J-coin [I] + 1 ;}} int ntotal = 0; for (I = 1; I <= nsum; I ++) {ntotal + = DP [I];} return ntotal;} int main () {int I, j; while (scanf ("% d", & ncoin, & nsum )! = EOF) {If (ncoin = 0 & nsum = 0) {break;} for (I = 0; I <ncoin; I ++) {scanf ("% d", coin + I) ;}for (I = 0; I <ncoin; I ++) {scanf ("% d ", amount + I);} printf ("% d \ n", getnumofpayments ();} return 0 ;}

The complexity of the Code is that O (N * m) should be a good solution ~

In short, today's gains are still great. I have figured out several basic types of backpack problems, AC has four related exercises, and I am confident in finding a job next.

Prepare to challenge FB's online puzzle tomorrow. Good luck ~

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.