[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
1.普通背包问题
2.完全背包问题
3.多重背包问题
1.普通背包问题
每到第i个位置就考虑是否拿第i个位置上的物品(当然也要考虑当前背包的体积是否能装得下这个物品), 以此类推,直至到达最后一个物品的位置。
#include <iostream> #include <algorithm> using namespace std; /* * 有 N
件物品和一个容量是 V 的背包。每件物品只能使用一次。 *第 i 件物品的体积是 vi,价值是 wi。
*求解将哪些物品装入背包,可使这些物品的总体积不超过背包容量,且总价值最大。 *输出最大价值。 */ const int N=1500; int
v[N],w[N]; int dp[N]; int main() { int m,n; cin>>m>>n; for(int i=1;i<=m;i++)
cin>>v[i]>>w[i]; for(int i=1;i<=m;i++) { for(int j=n;j>=v[i];j--) {
dp[j]=max(dp[j],dp[j-v[i]]+w[i]); } } cout<<dp[n]<<endl; return 0; }
2.完全背包问题
和普通背包的区别就在于每个物品的数量是无穷的,我们只需要多做一个循环来选择在第i个位置上拿几个物品即可,因为多了一组循环,会使时间复杂度较高,这个时候如果不优化就会出现超时的问题。
#include <iostream> #include <algorithm> using namespace std; const int
N=1001; int w[N],v[N]; int dp[N]; int main() { int m,n; cin>>m>>n; for(int
i=1;i<=m;i++) cin>>v[i]>>w[i]; for(int i=1;i<=m;i++) for(int j=v[i];j<=n;j++)
dp[j]=max(dp[j],dp[j-v[i]]+w[i]); cout<<dp[n]<<endl; return 0; }
3.多重背包问题
接下来是多重背包问题,在这里强调一下这个问题。感觉这个方法真的很神奇。
那么我们只需要将这个物品的数量以这个形式(二进制)的方法优化,就可以选择出任意数量的物品。这一步完成之后就把这道题当作一个普通的背包问题即可。
#include <iostream> #include <algorithm> using namespace std; const int
N=25000; int v[N],w[N]; int dp[N]; int main() { int m,n; cin>>m>>n; int ans=1;
int k,a,b,s; for(int i=1;i<=m;i++) { cin>>a>>b>>s; k=1; while(k<=s) {
v[ans]=k*a; w[ans]=k*b; s-=k; k*=2; ans++; } if(s>0) { v[ans]=s*a; w[ans]=s*b;
ans++; } } //接下来就相当于是一个普通的背包问题 //使用一维dp for(int i=1;i<ans;i++) for(int
j=n;j>=v[i];j--) dp[j]=max(dp[j],dp[j-v[i]]+w[i]); cout<<dp[n]<<endl; return 0;
}
今日份刷题!