当前位置:网站首页>Dynamic programming: grouping knapsack problem
Dynamic programming: grouping knapsack problem
2022-04-23 00:15:00 【anieoo】
ACwing 9.


and 01 Backpacks are similar
#include <iostream>
using namespace std;
const int N=110;
int f[N][N]; // Only once i Select from a group of items , The current volume is less than or equal to j The maximum of
int v[N][N],w[N][N],s[N]; //v Is the volume ,w For value ,s On behalf of the i Number of items in the group
int n,m,k;
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>s[i];
for(int j=0;j<s[i];j++){
cin>>v[i][j]>>w[i][j]; // Read in
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
f[i][j]=f[i-1][j]; // No election
for(int k=0;k<s[i];k++){
if(j>=v[i][k]) f[i][j]=max(f[i][j],f[i-1][j-v[i][k]]+w[i][k]);
}
}
}
cout<<f[n][m]<<endl;
}
Optimize :
#include <iostream>
using namespace std;
const int N=110;
int f[N]; // Only once i Select from a group of items , The current volume is less than or equal to j The maximum of
int v[N][N],w[N][N],s[N]; //v Is the volume ,w For value ,s On behalf of the i Number of items in the group
int n,m,k;
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>s[i];
for(int j=0;j<s[i];j++){
cin>>v[i][j]>>w[i][j]; // Read in
}
}
for(int i=1;i<=n;i++){
for(int j=m;j>=0;j--){
for(int k=0;k<s[i];k++){
if(j>=v[i][k]) f[j]=max(f[j],f[j-v[i][k]]+w[i][k]);
}
}
}
cout<<f[m]<<endl;
}
版权声明
本文为[anieoo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222157343827.html
边栏推荐
- Mitsubishi fx5u configures iebasic setting of mr-je-c servo driver
- MySQL安装及基本使用教程
- 99 multiplication table code
- 威伦触摸屏和倍福PLC通信报错AdsParseSymbol invalid array index
- 大型企业SAP集成WMS系统方案流程
- Difference between Beifu twincat1260 and tf5010 authorization
- 【ACM】47. 全排列 II(考虑某一树枝下,已经记录的元素在下一层递归时是否会被重复记录(使用数组used来记录))
- 什么是目标关键词?
- L1-069 tire pressure monitoring (15 points)
- 湖泊遥感研究进展(概述)
猜你喜欢

(transfer) Aspose Documentbuilder II of words Programming Guide

EXCEL IF函数的简单使用

SAP integrated WMS system solution process for large enterprises

Mitsubishi fx5u configures iebasic setting of mr-je-c servo driver

MTP manager development plan - study notes on day 1

(transfer) Aspose Words introduction

Download net framework process on Microsoft official website

(转)Aspose.words编程指南之DOM树结构初识,Node类继承关系及说明

(转)Win10上安装任意版本的.net framework

Implementation method of interface between bar code WMS system and ERP
随机推荐
三菱MR-JE-C伺服应用详细介绍
Node + mongoose paging effect
OJ daily practice -- monkey eating peach problem
MTP management training plan - study notes on day 2
常见的社群玩法盘点,你做的是哪一种?
Concurrent reachability analysis (three color marking method)
倍福EL6631和西门子通信profinet出现报错解决方案
Unittest单元测试(六)
Calculation of Beifu scaling factor
【ACM】90. Subset II (the de duplication problem of the same tree layer first needs to sort the array (use sort))
条码WMS系统的架构
In the b-end / C-end, which is more important, product or operation?
(turn) use dottrace6 0 for performance and memory analysis
Programming 2022-02 KTV
99 multiplication table code
Unittest unit test (6)
Absolute positioning does not use left, right, top, bottom and other attributes
MySQL安装及基本使用教程
Compared with the traditional anchor based method, fcos is unique
(转)Word2016怎么和mathtype兼容