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

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<iostream>
#include<stdio.h>
#include<algorithm>
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<N;i++)scanf("%d%d%d",&Goods[i].p,&Goods[i].q,&Goods[i].v);//然后需要对数组进行排序,需要注意的是,是按照q-p,并且是从小到大排序sort(Goods,Goods+N,cmp);//排完序之后就开始背包了for(i=0;i<N;i++)for(j=M;j>=Goods[i].q;j--)//要注意 这里是q而不是pf[j]=max(f[j],f[j-Goods[i].p]+Goods[i].v);printf("%d\n",f[M]);}
}

01背包入门:HDU 3466 Proud Merchants相关推荐

  1. 洛谷 P1048 采药 01背包入门

    P1048 采药 最近想学dp了 一道非常简单入门的01背包,写此博客做一个教训,对01背包还不是特别熟,动态方程很容易就推出来了,但是写二维状态方程时wa了,然后改为一维过了. 主要还是对01背包不 ...

  2. hdu 2602 01背包入门

    Many years ago , in Teddy's hometown there was a man who was called "Bone Collector". This ...

  3. 【动态规划-01背包】 HDU 1203 I NEED A OFFER!

    1203 ( I NEED A OFFER! )  Problem Description Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材料,于是,便需要去申请学 ...

  4. ☆【UVA - 624 】CD(dp + 0-1背包 + 记录路径)

    题干: You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music ...

  5. 51Nod 1085 01背包

    01背包入门题,刚学完当写模板. 在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2--Wn(Wi为整数),与之相对应的价值为P1,P2--Pn(Pi为整数).求背包能够容纳的最大价 ...

  6. POJ 3624 Charm Bracelet 0-1背包

    传送门:http://poj.org/problem?id=3624 题目大意:XXX去珠宝店,她需要N件首饰,能带的首饰总重量不超过M,要求不超过M的情况下,使首饰的魔力值(D)最大. 0-1背包入 ...

  7. DP专题 4 | 骨头收集爱好者 - POJ 1458( 0-1背包)

    背包问题是DP里面变化比较多的问题,可以参考网上的<背包9讲>,另外还是阅读<算竞入门>和<算竞进阶>,讲的最全的肯定是背包9讲,基本上把所有变形都讲了一遍,但是把 ...

  8. 【HDU - 3466 】Proud Merchants(dp,背包问题,巧妙排序)

    题干: Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and pow ...

  9. HDU - 3466(01背包理解)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3466 题意:给你一些钱 m ,然后在这个国家买东西, 共有 n 件物品,每件物品价格P价值V还有一个很 ...

最新文章

  1. css3 奇技淫巧 - 如何给小汽车换个背景色谈起
  2. 百练2815 城堡问题
  3. 2017西安交大ACM小学期数论 [更新学号]
  4. 宝塔面板服务器ip地址修改_「网站」快速搭建服务器环境及网站
  5. 我被喷了:这样写代码是多此一举?
  6. 开放性实践活动微信客服_什么是开放组织中的实践社区?
  7. Spark 性能相关参数配置详解-任务调度篇
  8. poj 1681 Painter's Problem (高斯消元 )
  9. linux网卡备份还原,CentOS6.x双网卡采用主-备份策略绑定(bond)
  10. java堆栈方法区_java 栈 ,堆, 方法区
  11. 切图具体需要切什么内容_什么是切图?网页制作中的切图是什么?
  12. mmap之内存映射文件与常规文件操作比较
  13. 监督学习、无监督学习、半监督学习、弱监督学习、强化学习
  14. 计量单位报错:消息号BM302 “未使用语言 ZH 创建单位 XXX”
  15. 【Python】xlwings-删除重复行
  16. 30分钟简易复刻元气骑士地图生成系统
  17. 学之思考试系统功能开发文档
  18. Interceptor for {http://admin.ws…thrown exception, unwinding now java.lang.NullPointerException
  19. 基于JSP的“爱心宠物诊所”系统课程设计
  20. 珍惜人生路上的点点滴滴

热门文章

  1. 无人机智能巡检软件设计
  2. 怎么在控制台下获取鼠标和键盘信息 即控制台下的键盘鼠标事件
  3. linux下使用firefox下载百度网盘资源的小技巧
  4. 区块链入门-带你搞懂区块链-熊丽兵-专题视频课程
  5. Java解决高并发秒杀商品
  6. Java转换成汇编asm程序
  7. 26 《基因组:人类自传》 -豆瓣评分9.1
  8. SQL数据库建表前期优化
  9. 途志;抖音这样在评论区引流,让你快速涨粉
  10. win10右键文件夹卡死