Hdu3466 Proud Merchants deformation 0-1 backpack

Source: Internet
Author: User

This question to understand the report to understand, the first encounter this deformation of the backpack, but it is really difficult to think of first sort by Q-P... Let's talk about my understanding of this question. First, let's talk about brute-force solutions. Obviously, we can use the enumeration method to make a choice for each item. However, even if there is a problem with violence, for example, for the items 3, 5, 5, 10, 5, and 5, if we choose neither of them or select only one of them, it is obviously no problem, however, if we select both of them, there are m> = 13 in the previous order, but if we change the order of the two, then m> = 10, here we can see the problem. Therefore, for any two items, I, j, to avoid the problem above, we can calculate the minimum amount required for the two orders. If I-> j, pi + Qj is required, and Pj + Qi is required for j-> I. If I-> j is a known result, Pi + Qj <Pj + Qi, that is, Qi-Pi> Qj-Pj. So if the previous items in the first order according to the Q-P from large to small, then the violence can be solved. However, this kind of violence can be solved by a better algorithm, namely, the 0-1 backpack. The original problem has been completely transformed into an ordinary 0-1 backpack, but there is still one thing to explain, I just said that the violence is arranged in order according to the Q-P from big to small, but because the dp and the violence are actually two inverse processes, the benefits of dp will not be explained more, that is, avoid repeated calculation of sub-problems. So we sort the order of Q-P from small to large before dp. OK, this question is finally solved! [Cpp] # include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace std; struct goods {int p, q, v, d; bool operator <(const goods t) const {return d <t. d ;}} g [501]; int dp [5001]; int main () {int n, m, I, j; while (~ Scanf ("% d", & n, & m) {memset (dp, 0, sizeof (dp); for (I = 1; I <= n; I ++) {scanf ("% d", & g [I]. p, & g [I]. q, & g [I]. v); g [I]. d = g [I]. q-g [I]. p;} sort (g + 1, g + n + 1); for (I = 1; I <= n; I ++) for (j = m; j> = g [I]. q; j --) dp [j] = max (dp [j], dp [j-g [I]. p] + g [I]. v); printf ("% d \ n", dp [m]);} 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.