首页 > 其他分享 >HDU 2191:悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 (多重背包)

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

时间:2022-11-14 21:33:06浏览次数:31  
标签:HDU pi int ci 2191 hi 512 dp 105


悼念512汶川大地震遇难同胞——珍惜现在,感恩生活


Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 25296    Accepted Submission(s): 10703


Problem Description


急!灾区的食物依然短缺!

为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格不等,并且只能整袋购买。


请问:你用有限的资金最多能采购多少公斤粮食呢?



后记:


人生是一个充满了变数的生命过程,天灾、人祸、病痛是我们生命历程中不可预知的威胁。


月有阴晴圆缺,人有旦夕祸福,未来对于我们而言是一个未知数。那么,我们要做的就应该是珍惜现在,感恩生活——


感谢父母,他们给予我们生命,抚养我们成人;


感谢老师,他们授给我们知识,教我们做人


感谢朋友,他们让我们感受到世界的温暖;


感谢对手,他们令我们不断进取、努力。 


同样,我们也要感谢痛苦与艰辛带给我们的财富~




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



 



Input


输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数n和m(1<=n<=100, 1<=m<=100),分别表示经费的金额和大米的种类,然后是m行数据,每行包含3个数p,h和c(1<=p<=20,1<=h<=200,1<=c<=20),分别表示每袋的价格、每袋的重量以及对应种类大米的袋数。


 



Output


对于每组测试数据,请输出能够购买大米的最多重量,你可以假设经费买不光所有的大米,并且经费你可以不用完。每个实例的输出占一行。


 



Sample Input


1 8 2 2 100 4 4 100 2


 



Sample Output


400


 



Author


lcy


 




多重背包的模版题目,今天刚刚学会多重背包的二进制优化,所以用这道题练习了一下!




AC代码:


#include<cstdio>
#include<cstring>
#include<iostream>
#define max(a,b) (a>b?a:b)
using namespace std;
int p[105],h[105],c[105];
int dp[105];
void solve(int pi,int hi,int n)
{
for(int i=n; i>=pi; i--)
dp[i]=max(dp[i],dp[i-pi]+hi);
}
void mult(int pi,int hi,int ci,int n)
{
int k=1;
while(k<=ci)
{
solve(k*pi,k*hi,n);
ci-=k;
k<<=1;
}
solve(ci*pi,ci*hi,n);
}
int main()
{
int C;
scanf("%d",&C);
while(C--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0; i<m; i++)
scanf("%d%d%d",p+i,h+i,c+i);
memset(dp,0,sizeof(dp));
for(int i=0; i<m; i++)
mult(p[i],h[i],c[i],n);
printf("%d\n",dp[n]);
}
return 0;
}

标签:HDU,pi,int,ci,2191,hi,512,dp,105
From: https://blog.51cto.com/u_15746719/5850783

相关文章

  • 题解 HDU4035 【Maze】
    postedon2022-08-1712:33:51|under题解|sourceproblemhttps://vjudge.net/problem/HDU-4035SHY在一棵树上随机游走,从根节点出发,每次有\(k_u\)的几率回到根节......
  • HDU 3726 Graph and Queries
    DescriptionYouaregivenanundirectedgraphwithNvertexesandMedges.Everyvertexinthisgraphhasanintegervalueassignedtoitatthebeginni......
  • HDU 3468 Treasure Hunting
    DescriptionDoyouliketreasurehunting?Today,withoneofhisfriend,iSeaisonaventuretripagain.Asmostmoviesaid,theyfindsomanygoldhid......
  • HDU 5446 Unknown Treasure
    ProblemDescriptionOnthewaytothenextsecrettreasurehidingplace,themathematiciandiscoveredacaveunknowntothemap.Themathematicianenter......
  • HDU 5434 Peace small elephant
    ProblemDescriptionXiaoMinglikesplayingtheinternationalchess,especiallyliketheelephant(itcanmovebyslantwhilenobarrier),buthethink......
  • HDU 5442 Favorite Donut
    ProblemDescriptionLuluhasasweettooth.Herfavoritefoodisringdonut.Everydayshebuysaringdonutfromthesamebakery.Aringdonutisconsis......
  • HDU 3518 Boring counting
    Description035nowfacedatoughproblem,hisenglishteachergiveshimastring,whichconsistswithnlowercaseletter,hemustfigureouthowmanysub......
  • HDU 4210 Su-domino-ku
    ProblemDescriptionAsiftherewerenotalreadyenoughsudoku-likepuzzles,theJuly2009issueofGamesMagazinedescribesthefollowingvariantthat......
  • HDU 5722 Jewelry
    ProblemDescriptionAfterallthedifficulties,PsycheandCupidarefinallygettingmarried.NoordinarypearlscanholdCupid'sloveforPsyche.So......
  • HDU 3713 Double Maze
    ProblemDescriptionUnlikesinglemaze,doublemazerequiresacommonsequenceofcommandstosolvebothmazes.Seethefigurebelowforaquickunderst......