当前位置:网站首页>ACM01背包问题
ACM01背包问题
2022-08-09 11:02:00 【抓个马尾女孩】
问题描述:给定N中物品和一个背包。物品i的重量是Wi,其价值位Vi,背包的容量为C。问应该如何选择装入背包的物品,使得转入背包的物品的总价值为最大?
f[i,j]表示在前i件物品中选择若干件放在承重为 j 的背包中,可以取得的最大价值。
Pi表示第i件物品的价值。
#include<iostream>
using namespace std;
const int maxn=2000;
int a[maxn][maxn],w[maxn],c[maxn];
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i=i+1)
cin>>w[i]>>c[i];
for(int i=1;i<=m;i=i+1)
for(int v=n;v>0;v=v-1)
{
if(w[i]<=v) a[i][v]=max(a[i-1][v],a[i-1][v-w[i]]+c[i]);
else a[i][v]=a[i-1][v];
}
cout<<a[m][n]<<endl;
return 0;
}
边栏推荐
猜你喜欢
The complete grammar of CSDN's markdown editor
性能测试(03)-JDBC Request
ThreadLocal及其内存泄露分析
Preparation for gold three silver four: how to successfully get an Ali offer (experience + interview questions + how to prepare)
FreeRTOS任务创建源码分析
Netscope:神经网络结构在线可视化工具
CentOS6.5 32bit安装Oracle-11gR2步骤说明
Arduino学习总结 + 实习项目
activemq 消息持久化
真香!肝完Alibaba这份面试通关宝典,我成功拿下今年第15个Offer
随机推荐
Invisible OOM in kubernetes
Julia资料收集
[Original] Usage of @PrePersist and @PreUpdate in JPA
1007 Maximum Subsequence Sum (25分)
Cesium加载三维模型数据
centos7.5 设置Mysql开机自启动
华为VRRP+MSTP联动接口检测实验案例
商业技术解决方案与高阶技术专题 - 数据可视化专题
二进制加法
focusablejs
[华为云在线课程][SQL语法分类][数据操作][学习笔记]
Official explanation, detailed explanation and example of torch.cat() function
Input and output of cnn
MySQL外键在数据库中的作用
electron 应用开发优秀实践
STM32启动方式及BootLoader
中断系统结构及中断控制
grpc系列-初探grpc 路由注册和转发实现
彻底理解工厂模式
tensorflow和numpy对应的版本,报FutureWarning: Passing (type, 1) or ‘1type‘ as a synonym of type is deprecate