HDOJ 2159 FATE (two-dimensional backpack)

Source: Internet
Author: User

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. I believe that with the Foundation above, you can implement the Program for this problem on your own.
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 [0, 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
{
For (l = 1; l <= s; l ++) // experience with <= l
Dp [j] [l] = dp [j] [l]> dp [j-B [I] [L-1] + a [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;
}


Author: ulquiorra0cifer

Related Article

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.