Hdoj 2159 fate (two-dimensional backpack)

Source: Internet
Author: User

Link: (-_-) ZZ

Train of Thought: according to the two-dimensional backpack problem described in backpack 9.

From lecture 9 of backpack:

Problem

The two-dimensional bag problem refers to: for each item, there are two different charges; the two costs must be paid at the same time when this item is selected; there is a maximum charge (Backpack capacity) for each price ). Ask how to select an item to maximize the value. Set these two costs to price 1 and price 2, respectively. The two costs required for item I are a [I] and B [I]. The maximum value (two types of backpack capacity) can be paid at two costs: V and U. The value of an item is W [I].

Algorithm

The fee is added to one dimension. You only need to add one dimension to the status. If f [I] [V] [u] is set, it indicates the maximum value that can be obtained when the cost of the first I item is V or U. The state transition equation is:

F [I] [V] [u] = max {f [I-1] [V] [u], f [I-1] [V-A [I] [U-B [I] + W [I]}

As mentioned above, only two-dimensional arrays can be used: when each item can only be retrieved once, the V and U variables adopt a backward loop, when an item is like a full backpack, use a sequential loop. Split an item when there are multiple backpack problems. Here we will not give any more false information.CodeI believe that with the Foundation above, you can achieve this problem on your own.Program.

Limit on the total number of items

Sometimes, the "two-dimensional fee" condition is given in an implicit way: a maximum of m items can be obtained. In fact, this is equivalent to an additional "number of items" for each item. The cost of each item is 1, and the maximum number of items that can be paid is M. In other words, if f [v] [m] is set, it indicates the maximum value that can be obtained when the cost is paid V and the maximum number of M items is selected. Then, according to the item type (01, complete, multiple) update cyclically using different methods, and then in F [0 .. v] [0 .. m.

Summary

When a problem is found to be deformed by a familiar dynamic planning question, adding a latitude to the original state to meet new restrictions is a common method. I hope you will first understand this method.

Code:

# Include <stdio. h> # include <string. h> int main () {int I = 0, j = 0, L = 0, n = 0, m = 0, K = 0, S = 0, flag = 0, A [102], B [102], DP [102] [102]; while (scanf ("% d", & N, & M, & K, & S )! = EOF) {for (I = 0; I <K; I ++) scanf ("% d", & A [I], & B [I]); memset (DP, 0, sizeof (DP); for (I = 0; I <K; I ++) {for (j = B [I]; j <= m; j ++) // patience to be spent currently {for (L = 1; L <= s; l ++) // experience DP when it is <= L [J] [l] = DP [J] [l]> DP [J-B [I] [L-1] + [I]? DP [J] [l]: DP [J-B [I] [L-1] + A [I] ;}} flag =-1; for (I = 1; I <= m; I ++) {If (flag! =-1) break; For (j = 1; j <= s; j ++) if (DP [I] [J]> = N) {flag = m-I; break;} If (flag =-1) printf ("-1 \ n"); else printf ("% d \ n ", flag);} return 0 ;}

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.