Codevs 3269 Hybrid Backpack

Source: Internet
Author: User

3269 Hybrid Backpack

time limit: 1 s space limit: 256000 KB title level: Diamonds Diamond

Title Description Description

Backpack volume is V, give n items, each item occupies a volume of VI, the value of WI, each item can take up to 1 pieces, or the maximum number of MI pieces (Mi > 1), or the amount of unlimited, in the total volume of the items installed under the premise of the value of the item is the largest value of the sum of what?

Enter a description Input Description

The first line two number n,v, the following n lines each row three number Vi,wi,mi represents each item's volume, the value and the quantity, Mi=1 represents the most takes one piece, mi>1 indicates to take the Mi piece, the mi=-1 indicates the quantity infinite

Output description Output Description

1 Number ans indicates the maximum value of the loaded item value

Sample input Sample Input

2 10

3 7 2

2 4-1

Sample output Sample Output

22

Data range and Tips Data Size & Hint

For 100% data, V <= 200000, N <= 200

1 //Mixed Backpack board, my brother's full tle~~~~~~~~ .2#include <iostream>3#include <cstring>4#include <cstdio>5 using namespacestd;6 #defineN 2107 #defineV 2000108 intN,v,f[v],wi[n],mi[n],vi[n];9 intMain ()Ten { Onescanf"%d%d",&n,&v); A      for(intI=1; i<=n;i++) -     { -scanf"%d%d%d",&vi[i],&wi[i],&mi[i]); the         if(mi[i]==-1) mi[i]=v/Vi[i]; -     } -      for(intI=1; i<=n;i++) -     { +          for(intk=1; k<=mi[i];k++) -         { +              for(intj=v;j>=vi[i];j--) AF[j]=max (f[j],f[j-vi[i]]+wi[i]); at         } -     } -printf"%d", F[v]); -     return 0; -}

AC Code:

1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 #defineN 2106 #defineV 2000107 intN,v,f[v],wi[n],mi[n],vi[n];8 intMain ()9 {Tenscanf"%d%d",&n,&v); One      for(intI=1; i<=n;i++) A     { -scanf"%d%d%d",&vi[i],&wi[i],&mi[i]); -         if(mi[i]==-1) mi[i]=v/Vi[i]; the     } -      for(intI=1; i<=n;i++) -     { -         intx=Mi[i]; +          for(intk=1; k<=x;k<<=1)//binary Optimization -         { +              for(intj=v;j>=vi[i]*k;j--) AF[j]=max (f[j],f[j-vi[i]*k]+wi[i]*k);  atx-=K;  -         } -       if(x) -          for(intj=v;j>=vi[i]*x;j--) -F[j]=max (f[j],f[j-vi[i]*x]+wi[i]*x); -     } inprintf"%d", F[v]); -     return 0; to}

Again, I will be able to go to countless times the number of items changed to v/vi[i] times (that is, by the total backpack volume to limit him, so that you can dispense with a step) ...

Binary optimization ~ ~ ~ still Slow

To be rescued ~~~~~~~

And there's a hairy relationship with the queue.

Codevs 3269 Hybrid 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.