Multi-pack--binary conversion method

Source: Internet
Author: User

Learn from God lzw,worship ...

Multiple backpacks (Multiplepack): There are n items and a backpack with a capacity of V. (i) Items with a maximum of n[i] pieces available, each cost is c[i], the value is w[i]. The solution of which items are loaded into the backpack allows the sum of the costs of these items to be no more than the backpack capacity and the maximum value.

This knapsack problem is similar to a complete knapsack problem. For article I items, you can take 0 pieces, 1 pieces, 2 pieces, ..., n[i] pieces. This problem can be turned into 01 knapsack problem, for taking 1,2,......,n[i] pieces of each piece of an article, so you can use 01 Knapsack method easy to solve. However, when strong data is encountered, this method can time out. The workaround is to use the binary attribute to represent N[i] as the 2^n format. In this way, the conversion can be completed, and the number of each item transformed from n[i] to log (N[i]), avoiding timeouts.

Give an example of:

Treasure Screening (treasure. Pas/c/cpp)

"Title description"

Finally, to solve the problem of the thousand years. Small FF found the Royal Treasure Room, filled with countless priceless treasures ... This little FF can be rich, quack. But the treasure is too much here, small FF collection car seems to not fit so many treasures. It seems that the small FF can only shed some of the treasures in tears ... Small ff sorted out the treasures in the cave, and he found that each treasure had one or more pieces. He gave a rough estimate of the value of each treasure, then began the Treasure screening work: Small FF has a maximum load of W of the acquisition vehicle, the cave has a total of n kinds of treasures, each treasure of the value of v[i], the weight of w[i], each treasure has m[i] pieces. Small FF hope in the acquisition of vehicles without overloading the premise, select some treasures into the collection car, make their value and maximum.

"Input Format"

The next n rows are three integers per line, where the first number of line I represents the value of Class I, the second integer represents the weight of the item, and the third integer is the number of items in that category.

The first behavior is an integer n and W, respectively, representing the number of species and the maximum load of the vehicle.

"Output Format"

The output is only an integer ans, indicating the maximum value of the treasures collected without overloading the collection vehicle.

"Input Sample"

4 20

3 9 3

5 9 1

9 4 2

8 1 3

"Output Example"

47

"Data Fan Circle"

For 30% of data: n≤∑m[i]≤10^4;0≤w≤10^3.

For 100% data: n≤∑m[i]≤10^5;

0 <w≤4*10^4:1≤n<100.

This is a typical multi-pack application. The code is given below:

varm,w,ms,i,k,a,b,c,j:longint;k2,k1,f:Array[0..1000000] ofLongint;functionMax (a,b:longint): Longint;begin        ifA>b Thenexit (a); Exit (b);End;beginassign (input,'treasure.in'); Assign (output,'Treasure.out'); reset (input); rewrite (output); READLN (M,W); MS:=0;  fori:=1  toM Do beginreadln (A,B,C); K:=1;  while(c>=k) Do beginInc (MS); K1[MS]:=a*k;k2[ms]:=b*K;                        Dec (c,k); K:=kSHL 1; End; if(c>0) Then beginInc (MS); K1[MS]:=a*c;k2[ms]:=b*C; End; End;  fori:=1  toMs Do                 forJ:=wDowntoK2[i] DoF[j]:=max (f[j],f[j-k2[i]]+K1[i]); Writeln (F[w]); close (input); close (output);End.

Multi-pack--binary conversion method

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.