HDU 2,191 Heavy Backpack, write your own template

Source: Internet
Author: User

Background: WA a few times, are small mistakes: put i--written i++ and the like, when writing must think of specific intentions. And you must write at least three sets of test data!!!!!!!

Learning: Templating to write multiple backpacks.

#include <cstdio> #include <iostream> #include <cstring>using namespace Std;int t,v,n;int c[109],w[ 109],num[109],f[109];void zeroonebag (int cost,int weight) {for (int i=v;i >= cost;i--) F[i]=max (F[i],f[i-cost]+weigh t);} void completebag (int cost,int weight) {for (int i=cost;i <= v;i++) F[i]=max (f[i],f[i-cost]+weight);}     void multiplybag (int cost,int weight,int num) {if (Cost*num >= v) completebag (cost,weight);        else{int k=1,m=num;            while (K < m) {zeroonebag (k*cost,k*weight);            M-=k;        k*=2;     } zeroonebag (M*cost,m*weight);    }}int Main (void) {scanf ("%d", &t);        while (t--) {scanf ("%d%d", &v,&n);        for (int i=0;i < n;i++) scanf ("%d%d%d", &c[i],&w[i],&num[i]);        memset (f,0,sizeof (F));        for (int i=0;i < n;i++) multiplybag (C[i],w[i],num[i]);    printf ("%d\n", F[v]); } return 0;}


HDU 2,191 Heavy Backpack, write your own template

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.