ACM_Complete Backpack

Backpack 3

Time Limit: 2000/1000ms (Java/Others)

Problem Description:

There are n items (each with an infinite number) whose weight and value are respectively Wi and Vi. Now select items whose total amount does not exceed W from these items, and find the maximum value of the sum of all solutions.

Input:

The input contains multiple sets of test cases, each of which starts with two-digit integers n and W (1<=n<=10000, 1<=W<=1000), followed by n lines, each with two-digit integers Wi, Vi (1<=Wi<=10000, 1<=Vi<=100)

Output:

The output is one row, the maximum value of the sum of the values ​​across all scenarios.

Sample Input:

3 4
1 2
2 5
3 7
3 5
2 3
3 4
4 5

Sample Output:

10
7 
problem-solving ideas: simple complete backpack. dp[j] represents the maximum value of the total value when the total weight of the picked items does not exceed j.
State transition equation: dp[j]=max(dp[j],dp[jw[i]]+v[i]).
AC code:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int w[10005],v[10005],dp[10005];
 4 int main()
 5 {
 6     int n,W;
 7     while(cin>>n>>W){
 8         memset(dp,0,sizeof(dp));
 9         for(int i=0;i<n;++i)
10             cin>>w[i]>>v[i];
11         for(inti= 0 ;i<n;++i){   // Number 
12              for ( int j=w[i];j<=W;++j)    // Weight enumerate from small to large 
13                  dp[j] =max(dp[j],dp[jw[i]]+ v[i]);
 14          }
 15          cout<<dp[W]<< endl;
 16      }
 17      return  0 ;
 18 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325413165&siteId=291194637