nyoj860 See also 01 backpack (backpack variant)

Source: Internet
Author: User

    • topic 860
    • topic information
    • run result
    • line
    • discussion area
See also 01 backpack time limit: +Ms | Memory Limit:65535KB Difficulty:3
Describe
There are n items with a weight and value of WI and VI, from which items with a total weight of not more than W are selected, and the maximum value of the sum of the items in all selection options is obtained. 1 <= N <=100 1 <= wi <= 10^7 1 <= vi <= 1 <= W <= 10^9
Input
multiple sets of test data.
Each set of test data is entered in the first row, N and W, followed by n lines, each line entered two numbers, representing the first item of WI and VI.
Output
meet the maximum value of test instructions, one row per group of test data.
Sample input
4 52 31 23 42 2
Sample output
7
Source
Drift Friendship Series
Uploaded by
Tc_ Zhang Friendship

If we use the previous backpack to do this problem, we will find that the mass range is too large to open the array. So you'll think of the conversion.

The problem is to ask for the least amount of quality to fit the maximum value. And we found that the Backpack value range is not big. You will think of the value of preserving quality.

#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;int main () {int n,w,wi[ 105],vi[105],dp[10005];while (scanf ("%d%d", &n,&w)!=eof) {int Sum=0;memset (dp,100,sizeof (DP));DP [0]=0;for ( int i=0;i<n;i++) scanf ("%d%d", &wi[i],&vi[i]), sum+=vi[i];for (int i=0;i<n;i++) {for (int j=sum;j>=vi[ i];j--)   dp[j]=min (Dp[j],dp[j-vi[i]]+wi[i]);} for (int j=sum;j>=0;j--) if (dp[j]<=w) {printf ("%d\n", j); return 0;} /*5 1010 510 610 710 110 2*/


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

nyoj860 See also 01 backpack (backpack variant)

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.