Knapsack Problem - 07 Depends on Knapsack

According to the order of "Nine Lectures on Backpacks", this article mainly focuses on the seventh lecture - Dependent Backpacks. The so-called "reliance on backpacks" means that the backpack objects have main accessories. Similar explanations have been mentioned in "Two-dimensional Cost Backpacks" before, but The main attachment here is a subordinate relationship, that is, to select the main component, you must select the attachment, and there can be many types of attachments, and the same attachment can also have attachments. The result and the situation considered will grow exponentially, so it cannot be written as an equation like "01 knapsack", but if given constraints, we can also convert it into something like "01 knapsack" or "complete knapsack", etc. form to solve.

The title designed in this article is the Huawei OJ question of Niuke.com - shopping list.

Problem Description:

Wang Qiang is very happy today, the company issued a year-end bonus of N yuan. Wang Qiang decided to use the year-end bonus for shopping. He divided the items he wanted to buy into two categories: main parts and accessories. The accessories belonged to a certain main part. The following table is some examples of main parts and accessories:
Main items Appendix
computer printer, scanner
bookcase books
desk desk lamp, stationery
work chair without
If you want to buy an item classified as an accessory, you must first buy the main item to which the accessory belongs. Each main piece can have 0, 1 or 2 attachments. Attachments no longer have attachments of their own. Wang Qiang wants to buy a lot of things. In order not to exceed the budget, he stipulates an importance level for each item, which is divided into 5 grades: expressed as an integer from 1  to  5, and the 5th grade is the most important. He also checked the price of each item (all in multiples of 10 yuan) from the Internet. He wants to maximize the sum of the products of the price and importance of each item under the premise of not exceeding N dollars (which can be equal to N dollars).
    Suppose the price of the jth item is v[j] , the importance is w[j] , k items are selected in total, and the numbers are j 1 , j 2 ,..., j k , then the required sum is:
v[j 1 ]*w[j 1 ]+v[j 2 ]*w[j 2 ]+ … +v[j k ]*w[j k ] . (where * is a multiplication sign)
    Please help Wang Qiang to design a shopping list that meets the requirements.

Enter description:

The first line of input, two positive integers, separated by a space: N m

(where N (<32000) is the total amount of money, m (<60) is the number of items you want to buy) From line 2 to line m+1, line j gives the item number j-1 Basic data, each row has 3 non-negative integers vpq (where v represents the price of the item (v<10000), p represents the importance of the item (1 ~ 5), and q represents whether the item is a main item or an accessory. If q=0, it means the item is the main piece, if q>0, it means the item is an accessory, q is the number of the main piece)

Output description:

The output file has only one positive integer, which is the maximum value (<200000) of the sum of the product of the price and the importance of the item that does not exceed the total amount of money.

The specific topics are as follows:

The specific Java implementation code is as follows:

//Here is explained with a one-dimensional array
	private static int bp_rely(int m,int n,int[] w,int[] v,int[] q){
		int[] c = new int[m+1];
		for (int i = 0; i < m+1; i++) {
			c[i] = 0;
		}
		List<Integer> list = new ArrayList<>();
		for (int i = 0; i < n; i++) {
			list.clear();
			if (q[i] == 0) {
				for (int j = m; j >= w[i]; j--) {
					list.add( Math.max(c[j-w[i]] + v[i], c[j]));
					c[j] = Math.max(c[j-w[i]] + v[i], c[j]);
				}
			}else {
				for (int j = m; j >= (w[i]+w[q[i]]); j--) {
					list.add(Math.max(c[j-w[i]-w[q[i]]] + v[q[i]], c[j]));
					c[j] = Math.max(c[j-w[i]-w[q[i]]] + v[q[i]], c[j]);
				}
			}
		}
		int max = Collections.max(list);
	return max;	
	}

In the above code, it should be noted that whether the object is a "main part" or an "accessory" is determined by judging whether q is "0", and if it is an accessory, the main part must be purchased before the accessory can be purchased, that is, the following code :

Math.max(c[jw[i]-w[q[i]]] + v[q[i]], c[j]) //w[i] and w[q[i]] are the main ones prices of parts and accessories

If there are any flaws or mistakes in the above, please criticize and correct.

Guess you like

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