博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
01背包入门:HDU 3466 Proud Merchants
阅读量:3898 次
发布时间:2019-05-23

本文共 1687 字,大约阅读时间需要 5 分钟。

这道题对我来说还是有点难度的,所以参考了两个博主的博客:

Problem Description

Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerful kingdom in the world. As a result, the people in this country are still very proud even if their nation hasn’t been so wealthy any more.
The merchants were the most typical, each of them only sold exactly one item, the price was Pi, but they would refuse to make a trade with you if your money were less than Qi, and iSea evaluated every item a value Vi.
If he had M units of money, what’s the maximum value iSea could get?

Input

There are several test cases in the input.
Each test case begin with two integers N, M (1 ≤ N ≤ 500, 1 ≤ M ≤ 5000), indicating the items’ number and the initial money.
Then N lines follow, each line contains three numbers Pi, Qi and Vi (1 ≤ Pi ≤ Qi ≤ 100, 1 ≤ Vi ≤ 1000), their meaning is in the description.
The input terminates by end of file marker.

Output

For each test case, output one integer, indicating maximum value iSea could get.

Sample Input

2 10
10 15 10
5 10 5
3 10
5 10 5
3 5 6
2 7 3

Sample Output

5
11

#include
#include
#include
using namespace std;//HDU3466Proud Merchants//这题在我看来是有点难的。首先输入的是 东西的数量和背包的容量//其次就是输入 东西的价格,价值,和一个特别的属性//所以需要先创建一个结构体数组struct goods{
int p,v,q;}Goods[100];//写一个比较方法,按照从小到大排序的话,cmp函数是一个自己定义的函数,返回值是bool型,它规定了什么样的关系才是“小于”,也就是排序条件bool cmp(goods a,goods b){
return (a.q-a.p)<(b.q-b.p);}int main(){
int f[100]={
0}; int N,M; int i,j; while(scanf("%d%d",&N,&M)!=EOF){
for(i=0;i
=Goods[i].q;j--)//要注意 这里是q而不是p f[j]=max(f[j],f[j-Goods[i].p]+Goods[i].v); printf("%d\n",f[M]); }}

转载地址:http://qkfen.baihongyu.com/

你可能感兴趣的文章
php面试题1-线程和进程的区别(顺带提下协程)
查看>>
php面试题2-用到过的传输协议
查看>>
php面试题3-yii2和yii的不一样的地方
查看>>
IOS 一些好的框架和 技术大牛的博客
查看>>
Java 和 Object-c的区别
查看>>
Windows环境下Android NDK环境搭建
查看>>
NDK Build 用法(NDK Build)
查看>>
Android NDK开发起步Hello Jni
查看>>
[已解决]AutoCompleteTextView 不显示匹配的内容,因为将空的内容添加进去了
查看>>
object c 归档和解档,其实就是java中的序列化和反序列化
查看>>
object c的浅拷贝(地址拷贝)和深拷贝(对象拷贝)
查看>>
object c son字符串的解析
查看>>
object c 非常强大的类的属性复制kcv键值码赋值
查看>>
Java中普通代码块,构造代码块,静态代码块区别及代码示例
查看>>
iOS 第4课 UILabel
查看>>
[已解决]junit.framework.AssertionFailedError: No tests found in
查看>>
“服务器端跳转”和“客户端跳转”的区别
查看>>
Datatables基本初始化——jQuery表格插件
查看>>
Servlet监听器——实现在线登录人数统计小例子
查看>>
Oracle笔记——简单查询语句 Oracle入门
查看>>