- 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)