HDU 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 (多重背包模板)

来源:互联网 发布:火鸟中文移动编程下载 编辑:程序博客网 时间:2024/06/05 16:42

 

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2191

题目的大意也很清楚,就是给你一定的钱,还有各种米的重量、费用

问你如何买才能买到最大重量的大米

 

AC代码,应用多重背包模板:

 

 

#include<cstdio>   #include<cstring> #include<iostream>using namespace std;#define max(a,b) a>b?a:b   int p[1050],h[1050],c[1050],dp[1050];  int n,m;    void ZeroOnePack(int cost,int weight)   //01背包{  int i;for(i=n;i>=cost;i--)   //n为总钱数,cost为开销dp[i]=max(dp[i],dp[i-cost]+weight); //}  void CompletePack(int cost,int weight)  //完全背包{  int i;for(i=cost;i<=n;i++)  dp[i]=max(dp[i],dp[i-cost]+weight);  }  void MultiplePack(int cost,int weight,int amount)   //多重背包,2机制算法,物品金额,重量,数量{  if(cost*amount>=n)  {  CompletePack(cost,weight);   //如果该物品的数量和单位金额总数大于总现金}  else  {  int k=1;  while(k<=amount)  {  ZeroOnePack(k*cost,k*weight);  //取k件amount-=k;  k<<=1;  //  *2 }  ZeroOnePack(amount*cost,amount*weight);  }         }  int main()  {      int C;      scanf("%d",&C);      while(C--)      {  int i;memset(dp,0,sizeof(dp));  scanf("%d %d",&n,&m);  //总钱数,种类  for(i=0;i<m;i++)  {  scanf("%d %d %d",&p[i],&h[i],&c[i]);  //p是价格,h是重量, c是数量MultiplePack(p[i],h[i],c[i]);       }  printf("%d\n",dp[n]);      }      return 0;  }  /*18 2   有的钱和品种2 100 4   价格,重量,数量4 100 2400怎么样买才能使重量最大*/


 

 

原创粉丝点击