动态规划解决POJ 3624
2011-09-23 22:23
246 查看
Description
Bessiehasgonetothemall'sjewelrystoreandspiesacharmbracelet.Ofcourse,she'dliketofillitwiththebestcharmspossiblefromtheN(1≤N≤3,402)availablecharms.EachcharmiinthesuppliedlisthasaweightWi(1≤Wi≤400),a'desirability'factorDi(1≤Di≤100),andcanbeusedatmostonce.BessiecanonlysupportacharmbraceletwhoseweightisnomorethanM(1≤M≤12,880).
Giventhatweightlimitasaconstraintandalistofthecharmswiththeirweightsanddesirabilityrating,deducethemaximumpossiblesumofratings.
Input
*Line1:Twospace-separatedintegers:NandM
*Lines
2..N+1:Linei+1describescharmiwithtwospace-separated
integers:WiandDi
Output
*Line1:Asingleintegerthatisthegreatest
sumofcharmdesirabilitiesthatcanbeachievedgiventheweight
constraints
SampleInput
SampleOutput
ViewCode
Bessiehasgonetothemall'sjewelrystoreandspiesacharmbracelet.Ofcourse,she'dliketofillitwiththebestcharmspossiblefromtheN(1≤N≤3,402)availablecharms.EachcharmiinthesuppliedlisthasaweightWi(1≤Wi≤400),a'desirability'factorDi(1≤Di≤100),andcanbeusedatmostonce.BessiecanonlysupportacharmbraceletwhoseweightisnomorethanM(1≤M≤12,880).
Giventhatweightlimitasaconstraintandalistofthecharmswiththeirweightsanddesirabilityrating,deducethemaximumpossiblesumofratings.
Input
*Line1:Twospace-separatedintegers:NandM
*Lines
2..N+1:Linei+1describescharmiwithtwospace-separated
integers:WiandDi
Output
*Line1:Asingleintegerthatisthegreatest
sumofcharmdesirabilitiesthatcanbeachievedgiventheweight
constraints
SampleInput
46 14 26 312 27
SampleOutput
23
ViewCode
#include<iostream> usingnamespacestd; intf[12881];//f[k]表示:当背包的容量为k时的最大价值 intmax(inta,intb) { returna>b?a:b; } intmain() { intNum,TotalWeight,i,k,j,weight[12881],value[12881]; cin>>Num>>TotalWeight; f[0]=0; for(i=0;i<Num;i++) cin>>weight[i]>>value[i]; for(i=0;i<Num;i++) for(k=TotalWeight;k>=weight[i];k--) { f[k]=max(f[k],(f[k-weight[i]]+value[i])); //在max中的两个参数f[k],和f[k-weight[i]]+value[i]都是表示在背包容量为k时的最大价值 //f[k]是这个意思,就不用说了。 //而f[k-weight[i]]+value[i]也表示背包容量为k时的最大价值是为什么呢? //首先,f[k-weight[i]]表示的是背包容量为k-weight[i]的容量,也就是说f[k-weight[i]] //表示的是容量还差weiht[i]才到k的价值,+walue[i]恰好弥补了差的这个价值。所以…… //如果你对f[k]=max(f[k],(f[k-weight[i]]+value[i]));这句话不是很清楚, //下面的这句代码会对你有帮助 //cout<<"i="<<i+1<<"f["<<k<<"]="<<f[k]<<endl; } cout<<f[TotalWeight]<<endl; return0; }
相关文章推荐
- 动态规划解决POJ 3624
- 0-1背包问题,poj 3624 Charm Bracelet动态规划-解题报告,增加最优路径构建
- POJ 题目3624 Charm Bracelet(动态规划 01背包)
- POJ 3624 Charm Bracelet(DP动态规划+滚动数组)
- poj 3311(浅谈状态压缩动态规划在解决TSP问题中的应用)
- poj 3624 (动态规划)
- POJ 3624 0-1背包问题 动态规划
- POJ3624 - Charm Bracelet - 动态规划之01背包
- POJ 3624 0-1背包问题 动态规划
- POJ-3624 Charm Bracelet(动态规划)
- POJ-3624-Charm Bracelet-简单0/1背包、动态规划、DP
- POJ - 3624 (01背包问题)(动态规划-滚动数组)
- POJ 3624 Charm Bracelet 动态规划(01背包问题)
- poj-1836-Alignment-动态规划
- poj 3624 Charm Bracelet
- POJ2373 Dividing the Path——动态规划+单调队列优化
- 0-1背包问题,用滚动数组,动态规划解决
- c++使用动态规划dp(自底向上)重构解决钢条切割输出最大收益和切割方案及运行实例结果
- poj 动态规划之1050 To the Max
- 状态压缩动态规划 POJ 2411 (编程之美-瓷砖覆盖地板)