Full backpack time limit: 3000 MS | memory limit: 65535 kb difficulty: 4
-
Description
-
A full backpack defines n items and a backpack with a capacity of V. Each item has an unlimited number of items available. The volume of item I is C and the value is W. Solving which items are loaded into a backpack can make the total size of these items not exceed the total size of the backpack, and the total value is the largest. This question requires that when a backpack is filled with a backpack, find the sum of the maximum value. If it cannot be filled with a backpack, output No
-
Input
-
Row 1: N indicates the number of groups of test data (n <7 ).
Next, the first row of test data in each group contains two integers, M and V. M indicates the number of item types, and V indicates the total size of the backpack. (0 <m <= 2000,0 <v <= 50000)
Each row in the next m row has two integers C, W representing the weight and value of each item respectively (0 <C <100000, 0 <W <)
-
Output
-
Corresponding to the output results of each group of test data (if it can be filled with a backpack, the maximum value of the items in the backpack is output when it is filled with a backpack. If it cannot be filled with a backpack, output No)
-
Sample Input
-
21 52 22 52 25 1
-
Sample output
-
NO1
-
Uploaded
-
ACM _ Zhao minghao
-
Problem solving: RT
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long10 using namespace std;11 const int INF = INT_MAX>>2;12 int c[2001],w[2001],dp[50001];13 int main(){14 int kase,n,i,j,v,k;15 scanf("%d",&kase);16 while(kase--){17 scanf("%d %d",&n,&v);18 for(i = 1; i <= n; i++)19 scanf("%d %d",c+i,w+i);20 for(i = 0; i <= v; i++)21 dp[i] = -INF;22 dp[0] = 0;23 for(i = 1; i <= n; i++){24 for(j = c[i]; j <= v; j++)25 if(dp[j] < dp[j-c[i]]+w[i]) dp[j] = dp[j-c[i]]+w[i];26 }27 if(dp[v] > 0){28 printf("%d\n",dp[v]);29 }else puts("NO");30 }31 return 0;32 }
View code