Complete backpack problem (Classic 01 backpack upgrade version)

Complete knapsack problem

There are N types of items and a backpack with a capacity of V, each item has unlimited pieces available.

The volume of the i-th item is vi and the value is wi.

Find out which items to load into the backpack so that the total volume of these items does not exceed the backpack capacity and the total value is the largest.
Output the maximum value.

Input format The
two integers in the first line, N and V, are separated by a space, indicating the number of objects and the volume of the backpack respectively.

Next, there are N rows, each with two integers vi and wi, separated by spaces, which represent the volume and value of the i-th item.

Output format
Output an integer that represents the maximum value.

Data range
0<N,V≤1000
0<vi,wi≤1000
Input example
4 5
1 2
2 4
3 4
4 5
Output example:
10

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
    
    
    int n,v,a[1010],b[1010],i,j,k,s,d[1010];
    scanf("%d%d",&n,&v);
    for(i=1;i<=n;i++)
    scanf("%d%d",&a[i],&b[i]);
	for(i=0;i<=v;i++)
	d[i]=0;
	for(i=1;i<=n;i++)
	for(j=a[i];j<=v;j++)
	{
    
    
		d[j]=max(d[j],d[j-a[i]]+b[i]);
	}
	printf("%d\n",d[v]);
}

**

There is no difference between a complete backpack and a 01 backpack. The complete backpack can be selected infinitely, while the 01 backpack can only be selected once, so we only need to start the loop j from a[i], and this item will be selected infinitely.

**

Insert picture description here
When i=1, j=a[i]=1, then the backpack capacity gradually increases from 1-5, when j=1, the backpack can hold the next first item, and so on when j=5 , Can hold five.
When i=2, j=a[i]=2. At this time, we find that the second item has the same value as the first item. In fact, it can be seen that the average value of the first item and the second item are the same a.
not going to push his own understanding ...

Guess you like

Origin blog.csdn.net/m0_46381590/article/details/111479669