洛谷P2871-[USACO07DEC]Charm Bracelet S(01背包模板题)

news/2024/10/6 19:30:10

题目描述:

Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the supplied list has a weight Wi (1 ≤ Wi ≤ 400), a 'desirability' factor Di (1 ≤ Di ≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than M (1 ≤ M ≤ 12,880).

Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.

有N件物品和一个容量为V的背包。第i件物品的重量是c[i],价值是w[i]。求解将哪些物品装入背包可使这些物品的重量总和不超过背包容量,且价值总和最大。

输入格式:

* Line 1: Two space-separated integers: N and M

* Lines 2..N+1: Line i+1 describes charm i with two space-separated integers: Wi and Di

第一行:物品个数N和背包大小M

第二行至第N+1行:第i个物品的重量C[i]和价值W[i]

输出格式:

* Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints

输出一行最大价值。

输入输出样例:

输入 #1复制

4 6
1 4
2 6
3 12
2 7

输出 #1复制

23

AC Code: 

#include<bits/stdc++.h>
using namespace std;
#define N 15000
int m,n,dp[N],weight[N],value[N];
int main() {
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++) {
		scanf("%d %d",&weight[i],&value[i]);
	}
	for(int i=1;i<=n;i++) {
		for(int j=m;j>=weight[i];j--) {
			dp[j]=max(dp[j],dp[j-weight[i]]+value[i]);
		}
	}
	printf("%d\n",dp[m]);
	return 0;
}


http://www.niftyadmin.cn/n/712535.html

相关文章

python编写阅卷软件_利用Python开发智能阅卷系统

1 importnumpy as np2 importargparse3 importimutils4 importcv25 #设置参数6 ap argparse.ArgumentParser()7 ap.add_argument("-i", "--image", default"test_01.png")8 args vars(ap.parse_args())9 #正确答案10 ANSWER_KEY {0: 1, 1: 4, 2…

Python札记2:None

在Python中&#xff0c;关键字None代表空值&#xff0c;也就是“什么都没有”的意思。None和数字 0、False、空字符串都不同&#xff0c;None是NoneType类型的单例对象&#xff0c;而且只有None能够是NoneType类型。使用内置函数type可以查看标识符的类型&#xff1a; >>…

VMware ESXi 5 “基于源虚拟端口ID的路由”

原文地址http://bbs.vmsky.com/thread-35010-1-1.html最近对esx5i负载均衡策略中的默认“基于源虚拟端口ID的路由”做了一些分析&#xff0c;非常有意思分享给大家。 先描述一下场景&#xff1a; 一台ESX5i服务器有6台VM&#xff08;姑且用VM1、VM2、VM3、VM4、VM5、VM…

006.MFC_对话框_复选框_单选钮

对话框和控件复选框单选框分组框示例&#xff1a;三原色画图 一、建立名为Demo2的MFC工程&#xff0c;按照下图添加控件 并修改2个Group Box Caption属性分别为颜色、外观 修改3个Check Box Caption和ID属性分别为(红色,IDC_CHK_RED)、(绿色,IDC_CHK_GREEN)、(蓝色,IDC_CHK_BLU…

Double,Float精度丢失

2019独角兽企业重金招聘Python工程师标准>>> 公司项目只关于一个单价的传递&#xff0c;经过加加减减&#xff0c;出现精度丢失的情况&#xff0c;需要用BigDecimal来消除精度丢失的问题。 转载于:https://my.oschina.net/miaojiangmin/blog/1031398

php判断mysql_query更新_[转]php判断mysql_query是否成功执行

针对update 语句等会对数据表进行修改的语句在mysql_query($sql);后面加上$result mysql_affected_rows();如果$result 值为-1表明语句没有成功执行&#xff0c;可能是语句格式有问题等等&#xff1b;如果$result 值为0 表明语句成功执行&#xff0c;但是update并没有改变数据…

oracle学习笔记(十) 查询练习(一)

查询练习一 表创建 create table employee as select * from soctt.emp ; --记得授权 sysdba用户登录 grant select on scott.emp to $username$--表结构 create table empployee_demo(empno number(4) not null primary key, --员工编号&#xff0c;主键ename varcha…

Python札记3:可变对象和不可变对象

Python中有可变对象和不可变对象之分。可变对象创建后可改变但地址不会改变&#xff0c;即变量指向的还是原来的变量&#xff1b;不可变对象创建之后便不能改变&#xff0c;如果改变则会指向一个新的对象。 Python中dict、list是可变对象&#xff0c;str、int、tuple、float是…