"Huawei OJ" "Algorithm Total chapter" "Huawei OJ" "068-shopping list" "Project Download" topic description
王强今天很开心,公司发给N元的年终奖。王强决定把年终奖用于购物,他把想买的物品分为两类:主件与附件,附件是从属于某个主件的,下表就是一些主件与附件的例子: 主件 附件 电脑 打印机,扫描仪 书柜 图书 书桌 台灯,文具 工作椅 无 如果要买归类为附件的物品,必须先买该附件所属的主件。每个主件可以有 0 个、 1 个或 2 个附件。附件不再有从属于自己的附件。王强想买的东西很多,为了不超出预算,他把每件物品规定了一个重要度,分为 5 等:用整数 1 ~ 5 表示,第 5 等最重要。他还从因特网上查到了每件物品的价格(都是 10 元的整数倍)。他希望在不超过N 元(可以等于 N 元)的前提下,使每件物品的价格与重要度的乘积的总和最大。 设第 j 件物品的价格为 v[j] ,重要度为 w[j] ,共选中了 k 件物品,编号依次为 j1 , j2 ,……, jk,则所求的总和为: v[j 1 ]*w[j 1 ]+v[j 2 ]*w[j 2 ]+ … +v[j k ]*w[j k ] 。(其中 * 为乘号) 请你帮助王强设计一个满足要求的购物单。
Enter a description
输入的第 1 行,为两个正整数,用一个空格隔开:N m(其中 N ( <32000 )表示总钱数, m ( <60 )为希望购买物品的个数。)从第 2 行到第 m+1 行,第 j 行给出了编号为 j-1 的物品的基本数据,每行有 3 个非负整数 v p q(其中 v 表示该物品的价格( v<10000 ), p 表示该物品的重要度( 1 ~ 5 ), q 表示该物品是主件还是附件。如果 q=0 ,表示该物品为主件,如果 q>0 ,表示该物品为附件, q 是所属主件的编号)
Output description
输出文件只有一个正整数,为不超过总钱数的物品的价格与重要度乘积的总和的最大值( <200000 )。
Input example
1000 5800 2 0400 5 1300 5 1400 3 0500 2 0
Output example
2200
Algorithm implementation
ImportJava.util.Scanner;/** * Idea: Given a shopping list, increase the amount and number of pieces progressively until the purchase order limit is reached (the number of items or the total amount); * The corresponding maximum product value is calculated progressively, and in this process, the maximum value of the product up to this level is compared * Author: Wang Junshu * time:2016-01 -24 08:30 * CSDN:HTTP://BLOG.CSDN.NET/DERRANTCM * Github:https://github.com/wang-jun-chao * declaration:all Rights Rese RVed!!! */ Public class Main { Public Static void Main(string[] astrings) {Scanner Scanner =NewScanner (system.in);//Scanner Scanner = new Scanner (Main2.class.getClassLoader (). getResourceAsStream ("Data2.txt")); while(Scanner.hasnext ()) {//Total amount of money intTotal = Scanner.nextint ();//The number of items you wish to purchase intnum = Scanner.nextint ();//Price per item int[] Price =New int[Num +1];//weights for each item int[] Value =New int[Num +1];//is the main part or the accessory int[] Check =New int[Num +1];//Each number is assigned 0price[0] =0; value[0] =0; check[0] =0;//Read input data for(inti =1; I <= num; i++) {Price[i] = Scanner.nextint (); Value[i] = Scanner.nextint (); Check[i] = Scanner.nextint (); }//result array //Line represents the number of items, the amount of money represented int[[] result =New int[Num +1][total +1];//The first column is assigned 0 for(intj =0; J <= Num; J + +) {result[j][0] =0; } for(inti =1; I <= total; i++) { for(intj =1; J <= Num; J + +) {//If it is an accessory if(Check[j] >0) {//RESULT[J-1][I-PRICE[J]] means the total amount of money used i-price[j], up to the maximum value of j-1 items //Total amount of money needed for more than (current item + his main piece) if(i > Price[j] + price[check[j]) {intW = result[j-1][I-PRICE[J]] + value[j] * Price[j]; Result[j][i] = w > Result[j-1][i]? W:result[j-1][i]; } }Else{//Total amount of money can be buyer pieces if(i >= price[j]) {intW = result[j-1][I-PRICE[J]] + value[j] * Price[j]; Result[j][i] = w > Result[j-1][i]? W:result[j-1][i]; }}}} System.out.println (Result[num][total]); } scanner.close (); }}
"Huawei OJ" "068-shopping list"