hdu2159 Two-dimensional backpack

Source: Internet
Author: User
Tags cmath

Topic Connection

Backpack Nine talk----full backpack

There are n items and a backpack with a capacity of V, with unlimited pieces available for each item. the cost of item I is C[i] and 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.

Basic ideas

The problem is very similar to the 01 knapsack problem , the difference is that each item has unlimited pieces. That is, from the point of view of each item, the strategy associated with it has not taken or not taken two kinds, but take 0 pieces, take 1 pieces, take 2 pieces ... And so many kinds. If you still follow the idea of 01 knapsack, make f[i][v] indicate that the first I item fits into a backpack with a capacity of v maximum weight. The state transfer equation can still be written according to the different strategies of each item, like this:

F[I][V]=MAX{F[I-1][V-K*C[I]]+K*W[I]|0<=K*C[I]<=V}

This is the same as the 01 knapsack problem with O (n*v) states need to solve, but the time to solve each state is not constant, the time to solve the state F[i][v] is O (v/c[i]), the total complexity is more than O (VN Of

The Basic idea of the 01 knapsack problem is improved, and a clear method is obtained. This shows that the equation for the 01 knapsack problem is really important and can be extended to other types of knapsack problems. But we're still trying to improve the complexity.

A simple and efficient optimization

Complete knapsack problem has a very simple and effective optimization, is this: if two items I,J satisfies C[i]<=c[j] and w[i]>=w[j], then the item J removed, do not consider. The correctness of this optimization is obvious: In any case, the value of the small cost of the high price of J to the inexpensive I, get at least not worse solutions. For randomly generated data, this method tends to significantly reduce the number of items, thus speeding up. This, however, does not improve the complexity of the worst case scenario, as it is possible that specially designed data can not be removed from a single item.

This optimization can be simple O (n^2) to achieve, generally can withstand. In addition, for the knapsack problem, a good way is:[obviously ] first to remove the cost of more than V, and then use a similar counting method, calculate the cost of the same items of the highest value, you can O (v+n) to complete this optimization. This less important process will not give the pseudo-code, I hope you can think independently write pseudo-code or program.

Conversion to 01 knapsack Problem Solving

Since 01 knapsack problem is the most basic knapsack problem, then we can consider the complete knapsack problem into 01 knapsack problem to solve. The simplest idea is that, considering the maximum number of v/c[i items I have , I can convert item i to v/c[i] items with the same cost and value, and then solve the 01 knapsack problem. This completely does not improve the time complexity of the basic idea, but this gives us the idea of turning the complete knapsack problem into a 01 knapsack problem: To split an item into multiple items.

A more efficient method of conversion is to split the item I into a fee of c[i]*2^k, the value of w[i]*2^k a number of items, where K satisfies c[i]*2^k<=v. This is the idea of binary, because no matter the optimal strategy selected a few items I, can always be expressed as a number of 2^k pieces of goods and. This is a lot better than breaking each item into an O (log (v/c[i)) piece.

But we have better algorithms for O (VN).

O (VN) algorithm

This algorithm uses a one-dimensional array, which looks at the pseudo-code first:

For I=1..N

For V=0..V

F[v]=max{f[v],f[v-cost]+weight}

You will find that this pseudo-code is associated withP01The pseudo-code is only V has a different cycle order. Why is such a change feasible? First think of why P01 to follow v=v. 0 in reverse order to cycle. This is because the state of the I-cycles is guaranteed F[i][v] is by the state f[i-1][ V-c[i]] "recursion comes. In other words, this is to ensure that each item is selected only once, to ensure that the " selected in i items " This strategy is based on a sub-result of a I-item F[i-1][v-c[i]. And now the full backpack is a unique feature of each item can choose unlimited pieces, so in consideration "Add a i items "This strategy requires a sub-result that may have been selected in the article item F[i][v-c[i]], so you can and must use the span lang= The sequential loop of the "en-us" > v=0..v. This is the reason why this simple procedure is set up.

DP[I][JJ] means to spend the endurance of I kill JJ only to gain the experience of the value

This problem is based on the solution Code of the O (VN) algorithm:

for (int jj=1;jj<=s;jj++) adds a constraint to the number of kills, and this question is not infinite for each strange number of monsters, just unknown
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6 using namespacestd;7 intdp[ the][ the];8 Const intinf=1e9;9 structMosterTen { One     intper; A     intend; -}moster[ the]; - intMain () the { -     intp, n,m,k,s,i,j,a,b; -      while(~SCANF ("%d%d%d%d",&n,&m,&k,&s)) -     { +p=0;intans=inf; -Memset (DP,0,sizeof(DP)); +          for(i=0; i<k;i++) Ascanf"%d%d",&moster[i].per,&moster[i].end); at          for(i=0; i<k;i++) -         { -              for(j=moster[i].end;j<=m;j++) -             { -                  for(intjj=1; jj<=s;jj++) -                 { inDp[j][jj]=max (dp[j][jj],dp[j-moster[i].end][jj-1]+moster[i].per); -                     if(dp[j][jj]>=n&&j<ans) to                     { +ans=J; -                     } the                 } *             } $         }Panax Notoginseng         if(ans==inf) -printf"-1\n"); the         Else +printf"%d\n", M-ans); A     } the     return 0; +}

Solution of F[i][v]=max{f[i-1][v-k*c[i]]+k*w[i]|0<=k*c[i]<=v} based on state transfer equation

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6 using namespacestd;7 intdp[ the][ the];8 Const intinf=1e9;9 structMosterTen { One     intper; A     intend; -}moster[ the]; - intMain () the { -     intp, n,m,k,s,i,j,a,b; -      while(~SCANF ("%d%d%d%d",&n,&m,&k,&s)) -     { +p=0;intans=inf; -Memset (DP,0,sizeof(DP)); +          for(i=0; i<k;i++) Ascanf"%d%d",&moster[i].per,&moster[i].end); at          for(i=1; i<=m;i++) -         { -              for(j=0; j<k;j++) -              for(intjj=1; jj<=s;jj++) -             { -                 intAnt=1; in                  while((i>=moster[j].end*ant) &&ant<=JJ) -                 { toant++; +Dp[i][jj]=max (dp[i][jj],dp[i-moster[j].end][jj-1]+moster[j].per); -                 } the             } *             if(dp[i][s]>=N) $             {Panax NotoginsengAns=i; Break; -             } the         } +         if(ans==inf) Aprintf"-1\n"); the         Else +printf"%d\n", M-ans); -     } $     return 0; $}
if (dp[i][s]>=n)
{
Ans=i;break;

}

Spend the endurance value of I kill the S value of the strange after the experience value to meet the upgrade required experience to jump out of the loop

The cost of endurance is the lowest, the topic only requires the lowest cost of endurance, does not require killing the number of monsters (of course, the maximum number of kills to meet the requirements of the problem)

hdu2159 Two-dimensional backpack

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.