Poj 1837 01 backpack Variant

Source: Internet
Author: User

Question address: http://poj.org/problem? Id = 1837

At the beginning of this question, there is really no idea. Even if someone says it is a 01 backpack, they still don't know how to replace the model with a 01 backpack.

In a classic backpack, items are generally composed of items (WI, vi). Wi indicates its weight and VI indicates its value. Then, a fixed-capacity backpack is provided, and items are selected to maximize the value.

1. Greedy algorithms when items can be separated

2. When there is only one item, it is a 01 backpack.

3. When there are numerous items, they are full backpacks.

4. Some backpacks are used when the item has a fixed part.

This topic is described as follows:

There is a balance, there are several hooks on the balance, give you a number of weights, how many ways to balance the balance.

I have been reading this question for half a day to understand what it means. I can see it all at once with the diagram.

The hook on the balance is the stuff shown in the red dot, and the weight is the green stuff. Some physical knowledge may be used here. The balance condition should be

Y-wi * di = Y-WJ * DJ.

I indicates the combination on the left, J indicates the right, w indicates the weight, and di indicates the distance to the center of the balance.

Torque = force * arm. The following formula is obtained based on the equilibrium state.

DP [I] [J + weights [I-1] * armlen [k] + = DP [I-1] [J];

It is also equivalent

DP [I] [J] + = DP [I-1] [J-weights [I-1] * armlen [k];

Now we describe the state of all the torque. DP [I] [J] indicates that when we have placed the I weight, the torque is the number of all the discharge methods of J.

If J is used directly, the problem may occur because the left torque is negative, but we can find a large number and add this number to all States J, A non-negative state is obtained.

Here we have a maximum weight of 25, a hook of up to 15, and a maximum of 20 weights. We can see that the maximum status is [-7500,750 0]. We can add 7500 to each status and change it to [0, 15000].

Source CodeProblem: 1837User: hopeztmMemory: 780KTime: 47MSLanguage: C++Result: AcceptedSource Code#include <stdio.h>#include <memory.h>#define MAX_STATUS 7500#define MAX_NUM 21int armLen[MAX_NUM];int weights[MAX_NUM];int nArm, nWeight;int DP[MAX_NUM][MAX_STATUS];int main(){int i,j; while(scanf("%d%d", &nArm, &nWeight) != EOF){memset(DP, 0, sizeof(DP));                //input for( i = 0; i < nArm; i++){scanf("%d", armLen + i);    }int weightSum = 0;for( i = 0; i < nWeight; i++){scanf("%d", weights + i);weightSum += weights[i];}                //get max and min status IDconst int maxStatus = armLen[nArm - 1] * weightSum;const int minStatus = armLen[0] * weightSum;DP[0][MAX_STATUS] = 1; // if we put 0 weight, and the balanced is balancedfor( i = 1; i <= nWeight; i++) // for all weights{for( j = minStatus; j <= maxStatus; j++){for( int k = 0; k < nArm ; k++){DP[i][j + weights[i-1]*armLen[k] + MAX_STATUS] += DP[i-1][j + MAX_STATUS]; }}}printf("%d\n", DP[nWeight][MAX_STATUS]);}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.