当前位置:网站首页>Luogu P1776: Treasure Screening ← Multiple Knapsack Problem Binary Optimization
Luogu P1776: Treasure Screening ← Multiple Knapsack Problem Binary Optimization
2022-08-06 12:24:00 【hnjzsyjyj】
[Title Source]
https://www.luogu.com.cn/problem/P1776
【Title Description】
Finally, the thousand-year-old problem has been solved.Xiao F found the treasure room of the royal family, which was filled with countless valuable treasures.
Now Xiao F can make a fortune, quack.But there are too many treasures here, and Xiao F's collection car seems to be unable to hold so many treasures.It seems that Xiao F can only abandon some of the treasures with tears.
Xiao F sorted out the treasures in the cave and found that each treasure has one or more pieces.He roughly estimated the value of each treasure, and then started the treasure screening work: Xiao F has a collection vehicle with a maximum load of W, and there are a total of n kinds of treasures in the cave. The value of each treasure is vi and the weight is wi,There are mi pieces of each treasure.Xiao F hopes to select some treasures to be loaded into the collection vehicle to maximize their value on the premise that the collection vehicle is not overloaded.
[Input format]
The first line is an integer n and W, which represent the number of treasure species and the maximum load of the collection vehicle, respectively.
The next n lines each contain three integers vi, wi, mi.
[Output Format]
The output is only an integer, which represents the maximum value of the treasure collected under the condition that the collection truck is not overloaded.
[Algorithm Analysis]
Multiple knapsack problems can usually be transformed into 01 knapsack problems to solve.However, if the quantity of each item is split into multiple 1s, the time complexity will be very high, resulting in TLE.Therefore, it is necessary to use binary optimizationidea.That is:
A positive integer n can be decomposed into1,2,4,…,2^(k-1),n-2^k+1.where k is the largest integer that satisfies n-2^k+1>0.
For example, assuming an item with a value of 2 and a quantity of 10, 10 can be decomposed into 1+2+4+3 according to the idea of binary optimization, then the original valueItems with a value of 2 and a quantity of 10 can be equivalently converted into items with a value of 1*2, 2*2, 4*2, 3*2, that is, items with a value of 2, 4, 8, 6, and a quantity of 1..
[Algorithm Code]
#include using namespace std;int n,V;const int maxn=100005;int vol[maxn],val[maxn];int c[maxn];int main() {cin>>n>>V;int cnt=0;for(int i=1; i<=n; i++) {int wi,vi,type;cin>>vi>>wi>>type;int k=1;while(k<=type) {cnt++;vol[cnt]=wi*k;val[cnt]=vi*k;type-=k;k*=2;}if(type>0) {cnt++;vol[cnt]=wi*type;val[cnt]=vi*type;}}n=cnt;for(int i=1; i<=n; i++) {for(int j=V; j>=vol[i]; j--)c[j]=max(c[j],c[j-vol[i]]+val[i]);}cout<
[References]
https://blog.csdn.net/hnjzsyjyj/article/details/109363826
https://www.luogu.com.cn/problem/P1776
边栏推荐
- 【Web3 系列开发教程——创建你的第一个 NFT(6)】为 NFT 设置价格
- Ansible自动化运维、ZABBIX监控
- NC1 Addition of Large Numbers
- Hu-cang integrated e-commerce project (1): project background and structure introduction
- 绝了!阿里人用7部分讲明白百亿级高并发系统(全彩版小册开源)
- 链表 | 双指针法 | 删除链表的倒数第 N 个结点 | leecode刷题笔记
- 微信模板消息跳转小程序
- 【Web3 系列开发教程——创建你的第一个 NFT(5)】使用 Ethers.js 铸造 NFT | 测试用例
- Nacos启动失败:Nacos Server did not start because dumpservice bean construction failure:No DataSource set
- 疫情期间去英国游学留学安全吗?签证转机、保险入关~
猜你喜欢

哈希表 | 基础知识总结

NC1 Addition of Large Numbers

违反常识追求流量 农产品带货直播乱象丛生

CISP-PTE Practical Exercises Explanation One (New Version)

Kubernetes stain and tolerance

How to find stills?Where can I get HD resources?It's too late to meet these 9 website channels!original

高性能云原生数据对象存储MinIO实战-上

分布式锁的应用实践 | 微服务申请逐渐递增且不重复的号码

IPv6地址规划

易知微数字孪生智慧港口|打造智能化调度综合管控“大脑”,实现港口建设“新升级”
随机推荐
QT: Using custom signals and slots
LeetCode 896. 单调数列
微服务架构 | 分布式事务 - [Seata]
架构实战营模块九作业
NASA suspends all spacewalks on the International Space Station due to safety issues with spacesuits
XML usage
Introduction to SQLNET.ALLOWED_LOGON_VERSION parameter in SQLNET.ORA
Unity工具类 ResourcesManager资源管理器
1408. 数组中的字符串匹配 : 简单模拟题
CISP-PTE实操练习题讲解一(新版)
哈希表 | 两个数组的交集 | leecode刷题笔记
PS6603-USB PD protocol SINK terminal output controller chip
【SSL集训DAY1】D【动态规划】【状态压缩】
苏秋贵:外贸企业如何做到以市场为导向
Kubernetes 开源未来
哈希表 | 快乐数 | leecode刷题笔记
rtklib-RINEX文件读取-rinex.c解析(一)
MD5【加密以及解密】
“恰好装满求最值”背包问题的初始化解析
How to find stills?Where can I get HD resources?It's too late to meet these 9 website channels!original