Description
Kim is very happy today, the home purchase of the new house on the key, the new house has a very spacious room of his own dedicated. To his delight, the mother said to him yesterday: "Your room needs to buy what items, how to decorate, you decide, as long as not more than n yuan money on the line." Early this morning, he began to make a budget, he wanted to buy items divided into two categories: the main parts and accessories, attachments are subordinate to a certain main parts, the following table is some examples of the main parts and accessories:
Main parts Accessories
Computer printers, scanners
Bookcase Books
Desk lamp, stationery
Work Chair No
If you want to buy an item that is classified as an accessory, you must first buy the main part that the attachment belongs to. Each main piece can have 0, one, or 2 attachments. Attachments no longer have attachments that belong to them. Jinming want to buy a lot of things, will certainly exceed the mother limit of N yuan. As a result, he set an important degree of each article, divided into 5, such as: The whole number of the 5th, and so the most important. He also found the price of each item (all multiples of 10 yuan) from the Internet. He hoped that the sum of the product of the price and the importance of each item would be the largest if not more than n yuan (which could equal N).
The price of the article J is v[j], the importance of w[j], a total of K items selected, the number is J1,J2,......,JK, then the sum is: v[j1]*w[j1]+v[j2]*w[j2]+ ... +v[jk]*w[jk]. (where * for multiplication sign) please help Jinming to design a shopping list that satisfies the requirements.
formatInput Format
The 1th line of the input file, which is two positive integers, is separated by a space:
Nm
where N (<32000) represents the total amount of money, M (<60) is the number of items you wish to purchase. )
From line 2nd to line m+1, line J gives basic data for items numbered J-1, with 3 non-negative integers per line
V P q
(where v indicates the price of the item (v<10000), p indicates the item's importance (), q indicates whether the item is a main part or an accessory. If q=0, indicates that the item is a main piece, if q>0, indicates that the item is an attachment, q is the number of the main part of the product)
output Format
The output file has only a positive integer, which is the maximum value of the sum of the product of the price and the importance of the item not exceeding the total amount of money
(<200000).
Example 1sample input 1[copy]
1000 5800 2 0400 5 1300 5 1400 3 0500 2 0
sample Output 1[copy]
2200
Limit
1s
Test Instructions: Some items need to buy a backpack to buy a front item.
idea: since there are only two attachments, it is easy to think of 4 transfer directions. Then it will be made into 01 backpacks. You can also use a tree-shaped DP solution.
/** @Date: 2016-12-10-19.25 * @Author: Lweleth ([email protected]) * @Link: https://github.com/* @Versi On: */#include <bits/stdc++.h> #define LL long long#define PII pair#define MP (x, y) Make_pair ((x), (y)) #define FI fir St#define se second#define PB (x) push_back ((x)) #define MMG (x) memset ((x), -1,sizeof (x)) #define MMF (x) memset ((x), 0, sizeof (x)) #define MMI (x) memset ((x), INF, sizeof (x)) using namespace Std;const int INF = 0x3f3f3f3f;const int N = 1e5+20;co NST double eps = 1e-8;int n, m;int V[n], p[n], q[n];int dp[80][35000];void TDP_DFS (int rt, int c) {if (C > 0) { for (int i = 1, i <= m; i++) {if (q[i] = = RT) {for (int j = 0; J <= C-v[i]; J + +)//backpack dp[i][j] = Dp[rt][j] + v[i] * P[i]; Tdp_dfs (i, c-v[i]);//Sub-tree is passed for (int j = 0; J <= C; j + +)/if (J >= V[i]) DP[RT][J] = max (Dp[rt][j], dp[i][j-v[i]]); }}}}int Main () {cin >> n >> m; for (int i = 1; I <= m; i++) {scanf ("%d%d%d", V + I, p + i, q + i); } MMF (DP); Tdp_dfs (0, N); printf ("%d\n", Dp[0][n]); return 0;}
Vijos 1313 jinming budget scheme tree DP