当前位置:网站首页>Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)
Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)
2022-04-23 04:39:00 【glorious_ dream】
Title Description :
a key : seek 01 In front of the backpack k The value of optimal solution and , And the backpack should be full .
Algorithm analysis :
First ,01 Backpacks should be familiar to everyone , We won't talk about transfer here .
Let's start with the first part : How to keep your backpack full ?
natural 01 The knapsack is from the array with an initial value of 0 Start , That is, every volume will transfer , It is possible to update the answer .
But if you want to fill it up , obviously , Insufficient volume ( No n The volume and energy of an object is this volume ) Obviously not . It seems difficult , But never mind. , We put f The array is first assigned to the minimum value .
What's the advantage of this ? Will find , Because it's painting down layer by layer , So the first layer obviously has only the volume, which is exactly the volume of the first article , The maximum value will be updated ( Because everything else is negative infinity , Will not update ).
Then start painting the second layer , obviously , Only the volume brushed on the first layer , Will then move back ( Because everything else is transferred from negative infinity , It makes no sense , It can't be the maximum ).
Then pay attention to ff The array should have an initial value of 0 ( Later on ).
Let's look at the second part : Seek before k The value of optimal solution and .
set up f[i][j] The volume is i, The first j The value of the optimal solution . At this time , Above said f The initial value of the array can be assigned . hold f[0][1] The assignment is 0, Because the volume is 0 The optimal solution is obviously 0.
We need to consider before k Excellent solution . Start with the optimal solution and look back .
01 Backpack is f[j] and f[j−w[i]]+v[i] The transfer of , Then this question can learn from this idea .
set up x,y The optimal solutions on both sides . If f[j][x]>f[j−w[i]][y], We use an array rnk Before deposit k Excellent solution , At this time rnk[++cnt]=f[j][x], And the x+1. On the contrary, the same is true .
What does this passage mean ? Is to record the capacity of these two backpacks , Which one is big .
Finally, put... Outside f The array has a volume of j Under the condition of k The optimal solution is assigned as rnk An array of former k Just one .
Master code :
#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://yzsam.com/2022/04/202204230402126205.html
边栏推荐
- leetcode006--查找字符串数组中的最长公共前缀
- [echart] Introduction to echart
- Interaction of diet gut microbiota on cardiovascular disease
- Summary of Android development posts I interviewed in those years (attached test questions + answer analysis)
- eksctl 部署AWS EKS
- 补:注解(Annotation)
- Create VPC in AWS console (no plate)
- IEEE Transactions on Systems, Man, and Cybernetics: Systems(TSMC)投稿须知
- 国外LEAD,联盟经理常见问答
- 229. Find mode II
猜你喜欢
Supplement: Annotation
test
【Echart】echart 入門
Summary of Android development posts I interviewed in those years (attached test questions + answer analysis)
[AI vision · quick review of robot papers today, issue 31] Fri, 15 APR 2022
win10, mysql-8.0.26-winx64.zip 安装
无线充电全国产化电子元件推荐方案
Why recommend you to study embedded
Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)
Introduction to Cortex-M3 register set, assembly language and C language interface
随机推荐
zynq平台交叉编译器的安装
520.检测大写字母
MYSQL50道基础练习题
[echart] démarrer avec echart
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
win10, mysql-8.0.26-winx64.zip 安装
兼容NSR20F30NXT5G的小体积肖特基二极管
数据孤岛是什么?为什么2022年仍然存在数据孤岛?
Stm32f4 MCU ADC sampling and FFT of ARM-DSP Library
Why recommend you to study embedded
[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022
FAQ of foreign lead and alliance Manager
STM32 upper μ C / shell transplantation and Application
2021数学建模国赛一等奖经验总结与分享
程序员抱怨:1万2的工资我真的活不下去了,网友:我3千咋说
Mysql---数据读写分离、多实例
【Echart】echart 入门
leetcode003--判断一个整数是否为回文数
How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
STM32 MCU ADC rule group multi-channel conversion DMA mode