01Backpack Introduction

The 01 knapsack problem (0/1 Knapsack problem) is a classic dynamic programming problem, which describes how to select a set of items to put into the knapsack so that the total value of the items is maximized under the given capacity constraints.

Problem Description:
Suppose there is a knapsack with capacity C. There are n items, and each item has two attributes: weight wi and value vi. We need to select some items to put in the backpack so that the total weight of the items in the backpack does not exceed C, and the total value is the largest.

Solution:
The 01 knapsack problem can be solved using dynamic programming. The specific steps are as follows:

1. Define a two-dimensional array dp, where dp[i][j] represents the maximum value of the first i items when the knapsack capacity is j.
2. Initialize the dp array, set both dp[0][j] and dp[i][0] to 0, which means that when the backpack capacity is 0 or there are no items to choose, the maximum value is 0.
3. For each item i (i from 1 to n), traverse the knapsack capacity j (j from 1 to C): -
   If the weight wi of the current item is greater than the knapsack capacity j, the item cannot be put into the knapsack, so dp [i][j] is equal to dp[i-1][j], indicating that the maximum value remains unchanged.
   - If the weight wi of the current item is less than or equal to the knapsack capacity j, there are two options:
     a) Do not put the item in the knapsack, and the maximum value at this time is dp[i-1][j].
     b) Put the item into the backpack. At this time, the maximum value is dp[i-1][j-wi] plus the value vi of the current item.
     Finally, the value of dp[i][j] takes the larger value of the above two choices.
4. Finally, dp[n][C] is the maximum total value under the given capacity limit.

You can use the backtracking method to find out which items are specifically selected and put into the backpack. Starting from dp[n][C], if dp[i][j] is equal to dp[i-1][j], it means that the i-th item is not put into the backpack; if dp[i][j] is equal to dp [i-1][j-wi] plus vi, it means that the i-th item is put into the backpack, then subtract wi from j, and continue to search forward.

Summary:
The 01 knapsack problem uses a two-dimensional array to record the optimal solution in different states through the method of dynamic programming, so as to obtain the maximum total value. The time complexity of this problem is O(nC), where n is the number of items and C is the capacity of the backpack.


Topic: picking medicine 

 code:

#include <bits/stdc++.h>

using namespace std;

int n, t;
int v[109];
int c[109];
int f[109][1000009];

int main () {
	
	cin >> t >> n;
	
	for (int i = 1; i <= n; i++) cin >> v[i] >> c[i];
	
	for (int i = 1; i <= n; i++)
		for (int j = 0; j <= t; j++) {
			f[i][j] = max (f[i - 1][j], f[i - 1][j - v[i]] + c[i]);
			if (j < v[i]) f[i][j] = f[i - 1][j];
		}
	
	cout << f[n][t];
	
	return 0;
}

Guess you like

Origin blog.csdn.net/Runcode8/article/details/131364226