Hdu2191 mourn the victims of the 512 Wenchuan earthquake-Cherish the present and be grateful for your life

Source: Internet
Author: User

Standard multi-backpack application, as long as you read the nine articles about the backpack, you should have done the question;

It is easier to use structs for comparison;

This problem is quite painful because the number is weighted in the middle and cannot be found after half a day. After reading PO3 several times, I still did not find it... with a strange heart, I searched for it. i'm going...

Alas. Pay attention to the habit of writing code later...

 1 #include<iostream> 2 #define maxn 105 3 #define max(a,b) (a>b?a:b) 4 int dp[maxn]; 5 int n,m,t; 6 struct node 7 { 8     int amount,weight,price; 9 }game[maxn];10 void zeroOnePack(int cost,int weight)11 {12     for(int i=n;i>=cost;i--)13         dp[i]=max(dp[i],dp[i-cost]+weight);14 }15 void completePack(int cost ,int weight)16 {17     for(int i=cost;i<=n;i++)18         dp[i]=max(dp[i],dp[i-cost]+weight);19 }20 void multiPack(int cost,int weight,int amount)21 {22     23       if(cost * amount >= n)24         completePack(cost,weight);25      else26     {27         int k=1;28         while( k< amount)29         {30             zeroOnePack(k*cost,k*weight);31             amount-=k;32             k*=2;33         }34         zeroOnePack(amount*cost,amount*weight);35      }36 }37 38 int main()39 {40     //freopen("2191.txt","r",stdin);41     int i,j;42     while(~scanf("%d",&t))43     {44         while(t--)45         {46             memset(dp,0,sizeof(dp));47             memset(game,0,sizeof(game));48             scanf("%d %d" ,&n,&m);49                 for(i=0;i<m;i++)50                     scanf("%d %d %d",&game[i].price,&game[i].weight,&game[i].amount);51              for(i=0;i<m;i++)52                 multiPack(game[i].price,game[i].weight,game[i].amount);53              printf("%d\n",dp[n]);54         }55     }56     return 0;57 }

 

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.