HDU 3496 (two-dimensional 01 backpack) Watch the movie

Source: Internet
Author: User
Tags define local

I want to watch more than N cartoons. She enjoys these cartoons to varying degrees and has different playback durations.

Her uncle can only buy m of them (not many are exactly m) for her. She asked her what is the maximum value she can get to watch cartoons within a limited period of time.

If she cannot watch the purchased cartoons within a limited period of time, the output is 0.

 

Here, I want to explain it clearly by using the handout given by Daniel's backpack 9.

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.

Like a one-dimensional 01 backpack, if it is in reverse order, it will save a dimension.

 

 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6  7 int dp[110][1010]; 8 const int INF = -9999999; 9 int t[110], v[110];10 11 int main(void)12 {13     #ifdef LOCAL14         freopen("3496in.txt", "r", stdin);15     #endif16 17     int T, n, m, l;18     scanf("%d", &T);19     while(T--)20     {21         scanf("%d%d%d", &n, &m, &l);22         for(int i = 0; i < n; ++i)23             scanf("%d%d", &t[i], &v[i]);24         for(int i = 0; i <= m; ++i)25         {26             for(int j = 0; j <= l; ++j)27             {28                 if(i == 0)    dp[i][j] = 0;29                 else dp[i][j] = INF;30             }31         }32         for(int i = 0; i < n; ++i)33             for(int j = m; j >= 1; --j)34                 for(int k = l; k >= t[i]; --k)35                     dp[j][k] = max(dp[j][k], dp[j-1][k-t[i]] + v[i]);36         if(dp[m][l] < 0)    dp[m][l] = 0;37         printf("%d\n", dp[m][l]);38     }39     return 0;40 }
Code Jun

 

HDU 3496 (two-dimensional 01 backpack) Watch the movie

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.