当前位置:网站首页>洛谷P1858 【多人背包】 (背包求前k优解)
洛谷P1858 【多人背包】 (背包求前k优解)
2022-04-23 04:02:00 【glorious_dream】

题目描述:
重点:求 01 背包前 k 优解的价值和,并且背包要装满。
算法分析:
首先,01 背包大家应该很熟悉了,这里就不讲转移式了。
先讲第一部分:如何保证背包装满?
正常的 01 背包是从数组初始值为 0 开始,也就是说每一种体积都会转移,有可能会更新答案。
但如果要装满,显然,装不满的体积(也就是没有 n 个物品的体积和能为这个体积)显然不能要。似乎感觉很难,但没关系,我们把 f 数组先赋值为最小值。
这样有什么好处?会发现,因为是一层一层往下刷,那么第一层显然只有体积正好为第一个物品的那个体积,会更新最大值(因为别的都是负无穷,不会更新)。
然后开始刷第二层,显然,只有在第一层刷过的那个体积,才会接着往后转移(因为转移别的都是从负无穷转移过来的,没有意义,不可能是最大值)。
然后注意 ff 数组要有一个初始值是 0 (后面讲)。
来看第二部分:求前 k 优解的价值和。
设 f[i][j] 表示体积为 i,第 j 优解的值。这时,上面说的 f 数组初始值就可以赋值了。把 f[0][1] 赋值为 0,因为体积为 0 的最优解显然是 0。
我们需要考虑前 k 优解。从最优解开始往后找。
01 背包是 f[j] 和 f[j−w[i]]+v[i] 的转移,那么这道题可以借鉴这种思路。
设 x,y 分别为两边的最优解。如果 f[j][x]>f[j−w[i]][y],我们用一个数组 rnk 来存前 k 优解,这时 rnk[++cnt]=f[j][x],并且把 x+1。反之同理。
这段话表示什么?就是记录这两种背包的容量,哪一种是大的。
最后在外面把 f 数组在容积为 j 的条件下的前 k 优解赋值为 rnk 数组的前 k 个就可以了。
总代码:
#include<bits/stdc++.h>
#define re register
using namespace std;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch == '-') f=-1 ; ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48) ; ch=getchar();}
return x*f;
}
inline void print(int x){
if(x/10) print(x/10);
putchar(x%10+'0');
}
const int M = 5010;
int k,value,n,ans;
int w[M],v[M],f[M][60],rnk[M];
signed main(){
k=read(),value=read(),n=read();
for(re int i(1) ; i<=n ; ++i) scanf("%d%d",&w[i],&v[i]);
memset(f,-0x7f7f7f7f,sizeof(f));
f[0][1] = 0;
for(re int i(1) ; i<=n ; ++i){
for(re int j(value) ; j>=w[i] ; --j){
int rn1=1,rn2=1,cnt=0;
while(cnt<=k){
if(f[j][rn1] > f[j-w[i]][rn2]+v[i]){
rnk[++cnt] = f[j][rn1];
rn1++;
}
else{
rnk[++cnt] = f[j-w[i]][rn2]+v[i];
rn2++;
}
}
for(re int h(1) ; h<=k ; ++h) f[j][h] = rnk[h];
}
}
for(re int i(1) ; i<=k ; ++i) ans += f[value][i];
printf("%d",ans);
return 0;
}
版权声明
本文为[glorious_dream]所创,转载请带上原文链接,感谢
https://blog.csdn.net/glorious_dream/article/details/124344817
边栏推荐
- 【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..
- Overview of knowledge map (II)
- Single chip microcomputer serial port data processing (1) -- serial port interrupt sending data
- 伦敦银最新价格走势图与买卖点
- Now is the best time to empower industrial visual inspection with AI
- Paddlepaddle does not support arm64 architecture.
- What is software acceptance testing? What are the benefits of acceptance testing conducted by third-party software testing institutions?
- [echart] Introduction to echart
- Machine translation baseline
- Express middleware ① (use of Middleware)
猜你喜欢

Source code and update details of new instance segmentation network panet (path aggregation network for instance segmentation)

【测绘程序设计】坐标反算神器V1.0(附C/C#/VB源程序)

Overview of knowledge map (II)

Summary of knowledge map (3)

How Zotero quotes in word jump to references / hyperlink

Network principle | connection management mechanism in TCP / IP important protocol and core mechanism

STM32单片机ADC规则组多通道转换-DMA模式
![[AI vision · quick review of robot papers today, issue 28] wed, 1 Dec 2021](/img/c8/90d020d192fe791c4dec5f4161e597.png)
[AI vision · quick review of robot papers today, issue 28] wed, 1 Dec 2021
![[AI vision · quick review of robot papers today, issue 29] Mon, 14 Feb 2022](/img/a3/88b20f3e1be702f580169400e417f4.png)
[AI vision · quick review of robot papers today, issue 29] Mon, 14 Feb 2022

QT program integration easyplayer RTSP streaming media player screen flicker what is the reason?
随机推荐
Win10 boot VMware virtual machine boot seconds blue screen problem perfect solution
Xshell、Xftp连接新创建的Unbutu系统虚拟机全流程
ROS series (IV): ROS communication mechanism series (1): topic communication
现货黄金操作技巧_估波曲线
Install PaddlePaddle on ARM
Network principle | connection management mechanism in TCP / IP important protocol and core mechanism
[latex] formula group
【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..
Paddlepaddle model to onnx
(valid for personal testing) compilation guide of paddedetection on Jetson
Retrieval question answering system baseline
What if you encounter symbols you don't know in mathematical formulas
Cortex-M3寄存器组、汇编语言与C语言的接口介绍
Set经典小题目
【BIM入门实战】Revit中的墙体层次以及常见问题解答
ROS series (V): common commands in ROS
Source code and update details of new instance segmentation network panet (path aggregation network for instance segmentation)
Detailed explanation on the use of annotation tool via (VGg image annotator) in mask RCNN
ROS series (II): ROS quick experience, taking HelloWorld program as an example
STM32F4单片机ADC采样及ARM-DSP库的FFT