See also 01 backpackTime limit: ms | Memory limit:65535 KB 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
-
-
-
-
Analysis:
-
-
Alternative 01 backpack, due to the range of values for a much smaller range of weight
The minimum weight and total value of a backpack with a value of J Dp[j] is sum
01 knapsack problem, the value of the title is understood as weight, and weight can be understood as value
Note that this is understood to be exactly full
-
-
-
-
on the code 、--
-
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;#defineN 100010intn,s;intW[n];intV[n];intDp[n];intMain () {inti,j; while(SCANF ("%d%d", &n,&s)! =EOF) { intsum=0; for(i=1; i<=n;i++) {scanf ("%d%d",&w[i],&V[i]); Sum+=V[i]; } memset (DP,0x3f,sizeof(DP)); dp[0]=0; for(i=1; i<=n;i++) { for(j=sum;j>=v[i];j--) {Dp[j]=min (dp[j],dp[j-v[i]]+W[i]); } } for(j=sum;j>=0; j--) { if(dp[j]<=s) {cout<<j<<Endl; Break; } } } return 0;}
Backpack [Nyoj 860] See also 01 backpack