HDU 2191 Mourning 512 Wenchuan earthquake--cherish now, Thanksgiving life (multiple backpack)

Source: Internet
Author: User

Test instructions: Give the maximum value of n, and then give the type M and each bag number C, price p, weight h, ask to buy the maximum weight of rice

Idea: Each item has a fixed maximum number of times. For multiple knapsack problems. Convert to 01 backpack to do

The following methods are converted to 01 backpack to do

Idea 1: The item does not spread out, choose each kind of time, carries on the discussion, (equivalent to the vertical filling backpack v The column, one column one column fills

Code 1:KJ

#include <iostream>#include<stdio.h>#include<string.h>using namespacestd;intdp[ the];intMain () {intI,j,k,tem; intT,n,_v;//number of test cases, item type, backpack size    intc[ the],v[ the];//Cost, Value    intnum[ the];//number of items per item    intTC,TV;//cost of Split items, valuescanf"%d",&t);  while(t--) {scanf ("%d%d",&_v,&N); Memset (DP,0,sizeof(DP));  for(i=1; i<=n; ++i) scanf ("%d%d%d",&c[i],&v[i],&Num[i]); //////         for(i=1; i<=n; ++i) for(K=_v; k>=c[i];--k) for(j=1; j<=num[i]&&j*c[i]<=k; ++J)//It's a loop more than a 01 backpack.{TC=j*C[i]; TV=j*V[i]; TEM=dp[k-tc]+TV; if(Tem>dp[k]) dp[k]=tem; }        //printf"%d\n", Dp[_v]); }    return 0;}
View Code


Idea 2: The item spreads out and then handles 01 backpacks (the equivalent of a line filled with items across the line,

2.1 Simple split, the expansion of each of the items, both n pieces a items, the N-piece whether to select the operation

Code 2:JK

#include <iostream>#include<stdio.h>#include<string.h>using namespacestd;intdp[ the];intMain () {intI,j,k,tem; intT,n,_v;//number of test cases, item type, backpack size    intc[ the],v[ the];//Cost, Value    intnum[ the];//number of items per item    intTC,TV;//cost of Split items, valuescanf"%d",&t);  while(t--) {scanf ("%d%d",&_v,&N); Memset (DP,0,sizeof(DP));  for(i=1; i<=n; ++i) scanf ("%d%d%d",&c[i],&v[i],&Num[i]); //////         for(i=1; i<=n; ++i) for(j=1; j<=num[i]&&j*c[i]<=_v; ++J)//It's a loop more than a 01 backpack.                 for(K=_v; k>=j*c[i];--k) {tem=dp[k-c[i]]+V[i]; if(Tem>dp[k]) dp[k]=tem; }        //printf"%d\n", Dp[_v]); }    return 0;}
View Code


hint:2.1 Code and Ideas 1 code is just a different loop order, the actual operation is the same. (and the complexity of the time is the same, slightly troublesome.)
Because
Idea 1:
for (K=_v; k>=c[i];--k)
for (j=1; j<=num[i]&&j*c[i]<=k; ++j)
Idea 2:
for (j=1; j<=num[i]&&j*c[i]<=_v; ++j)
for (K=_v; k>=j*c[i];--k)

Take a look at these two loops,
C[i] K _v
----------------------
1 |   .   .   .    . . |
|   .   .    . . |
J |   .    . . |
|    . . |
m | . |
---------------------()

The cycles of the two loops are the same and are all in the matrix '. ' , that is, the same number of calculations, the same complexity of time


2.2 Binary split, there are n pieces a goods, then split into 1,2,4,8,..., Q So, that is 1+2+4+...+q=n

Code 3:

#include <iostream>#include<stdio.h>#include<string.h>using namespacestd;intdp[ the];intMain () {intI,j,k,tem; intT,n,_v;//number of test cases, item type, backpack size    intc[ the],v[ the];//Cost, Value    intnum[ the];//number of items per item    intTC,TV;//cost of Split items, valuescanf"%d",&t);  while(t--) {scanf ("%d%d",&_v,&N); Memset (DP,0,sizeof(DP));  for(i=1; i<=n; ++i) scanf ("%d%d%d",&c[i],&v[i],&Num[i]); //////         for(i=1; i<=n; ++i) { for(j=1; j<=num[i]; num[i]=num[i]-j,j=j*2)//It's a loop more than a 01 backpack.{TC=j*c[i];//cost of items after splittingTv=j*v[i];//                 for(K=_v; k>=tc;--k) {tem=dp[k-tc]+TV; if(Tem>dp[k]) dp[k]=tem; }            }            if(num[i]>0)//If there is an item, Num[i] is the equivalent of Q in 1+2+4+...+q{TC=num[i]*C[i]; TV=num[i]*V[i];  for(K=_v; k>=tc;--k) {tem=dp[k-tc]+TV; if(Tem>dp[k]) dp[k]=tem; }            }        }    //printf"%d\n", Dp[_v]); }    return 0;}
View Code

HDU 2191 Mourning 512 Wenchuan earthquake--cherish now, Thanksgiving life (multiple 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.