Dependent knapsack problem-shopping list

Source: Internet
Author: User

Title:Wang Qiang is very happy today, the company to the N-dollar year-end award. Wang Qiang decided to use the year-end award for shopping, he divided the items he wanted to buy into two categories: the main parts and accessories, the attachment is from one of the main parts, the following table is some examples of the main parts and accessories:
Main parts Attachment
Computer Printers, scanners
Bookcase Books
Desk 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. Wang Qiang wanted to buy a lot of things, in order not to exceed the budget, he set each item an important degree, divided into 5, etc.: With an integer 1 5 indicates that the 5th and 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 are selected, numbered J 1, J 2, ..., J K, then the sum is: v[j 1]*w[j 1]+v[j 2]*w[j 2]+ ... +v[j k]*w[j K]. (where * for multiplication sign) Please help Wang Qiang design a shopping list to meet the requirements. Input:

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

(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 the basic data for the item numbered j-1, with 3 non-negative integer v p q per line (where v represents the price of the item (v<10000), and P indicates the importance of the item (1 5), q Indicates whether the item is a main piece or an attachment. 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:The output file has only a positive integer, which is the maximum value (<200000) of the sum of the price and the importance of the item that does not exceed the total amount of money. Example: 5-2 0 5 1 -5 1-3 0-2 0 2200 idea: The title contains the main parts and accessories, then the total form has main parts, main parts + accessories 1, main parts + accessories 2, main parts + accessories 1+ Attachment 2, convert it into knapsack problem.
Importjava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Map.Entry;ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {Scanner Scanner=NewScanner (system.in); intsum=Scanner.nextint (); intn=Scanner.nextint (); List<Item> extraitems=NewArraylist<main.item>(); Map<integer, group> groupmap=NewHashmap<integer, main.group>();  for(inti=0;i<n;i++)        {            intPrice=Scanner.nextint (); intdeg=Scanner.nextint (); intq=Scanner.nextint (); Item Item=NewItem (); Item.price=Price ; Item.value=price*deg; Item.id= (q==0?i+1: Q); if(q==0)//Main parts{Group Group=NewGroup (); Group.item=item;            Groupmap.put (item.id, group); }            Else             {                if(Groupmap.get (q)! =NULL) {Group Group=Groupmap.get (q); if(group.accessory!=NULL) {Group.accessory.add (item); }                    Else{group.accessory=NewArraylist<main.item>();                    Group.accessory.add (item); }                }                Else{Extraitems.add (item); }            }        }        //join in the wrong order         for(intI=0;i<extraitems.size (); i++) {Item Item=Extraitems.get (i); intId=item.id; if(Groupmap.get (id)! =NULL) {Group Group=groupmap.get (ID); if(group.accessory!=NULL) {Group.accessory.add (item); }                Else{group.accessory=NewArraylist<main.item>();                Group.accessory.add (item); }            }        }        //Converting a dependent treasure problem into a knapsack problemList<item> bag=NewArraylist<main.item>(); Iterator<map.entry<integer, group>> iterator=Groupmap.entryset (). iterator ();  while(Iterator.hasnext ()) {Entry<Integer,Group> entry=Iterator.next (); intId=Entry.getkey (); Group Group=Entry.getvalue (); if(group.accessory==NULL) {bag.add (Group.item); }            Else if(Group.accessory.size () ==1) {Item Acitem=group.accessory.get (0); Item Item=Group.item;                Bag.add (item); Item NewItem=NewItem (); Newitem.price=item.price+Acitem.price; Newitem.value=item.value+Acitem.value;            Bag.add (NewItem); }            Else if(Group.accessory.size () ==2) {Item AC1=group.accessory.get (0); Item AC2=group.accessory.get (1); Item Item=Group.item;                Bag.add (item); Item N1=NewItem (); N1.price=item.price+Ac1.price; N1.value=item.value+Ac1.value; Item N2=NewItem (); N2.price=item.price+Ac2.price; N2.value=item.value+Ac2.value; Item N3=NewItem (); N3.price=item.price+ac2.price+Ac1.price; N3.value=item.value+ac2.value+Ac1.value;                Bag.add (N1);                Bag.add (N2);            Bag.add (n3); }        }        //planning for the backpackSystem.out.println (Getmax (Bag, bag.size (), sum));    Scanner.close (); }         Public Static intGetmax (list<item> List,integer Index,intExtramoney) {        if(index==0 | | extramoney==0)return0; if(List.get (index-1). Price>extramoney)//If money isn't enough        {            returnGetmax (list, index-1, Extramoney); }        Else         {            intM1=getmax (list, index-1, Extramoney); intM2=getmax (list, index-1, Extramoney-list.get (index-1). Price) +list.get (index-1). Value; returnMath.max (M1, M2); }    }        Static classItem {intID; intPrice ; intValue//the product of price and importance degree    }    Static classGroup {Item item; List<Item>accessory; }}

Dependent knapsack problem-shopping list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.