Proud Merchants

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 4039    Accepted Submission(s):
1677

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
 
Author
iSea @ WHU
 
Source
Recommend
zhouzeyong   |   We have carefully selected several
similar problems for you:  3460 3463 3468 3467 3465 
 
简单的01背包,排个序就好,代码很简单。
复制一下网上大神的思路:

因为如果一个物品是5 9,一个物品是5 6,对第一个进行背包的时候只有dp[9],dp[10],…,dp[m],再对第二个进行背包的时候,如果是普通的,应该会借用前面的dp[8],dp[7]之类的,但是现在这些值都是0,所以会导致结果出错。于是要想到只有后面要用的值前面都可以得到,那么才不会出错。设A:p1,q1 B:p2,q2,如果先A后B,则至少需要p1+q2的容量,如果先B后A,至少需要p2+q1的容量,那么就是p1+q2 > p2+q1,变形之后就是q1-p1 < q2-p2。

所以要针对每个属性的q-p来进行排序

题意:每个物品有p、q、v,三个属性,每个物品的话费为p,但是前提是必须有q,v则是得到的价值。

附上代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
int p,q,v;
} ss[]; int maxs(int a,int b)
{
return a>b?a:b;
}
bool cmp(node a,node b)
{
return a.q-a.p<b.q-b.p; //关键排序,详见上文分析
}
int main()
{
int n,m,i,j;
int dp[];
while(~scanf("%d%d",&n,&m))
{
for(i=; i<n; i++)
scanf("%d%d%d",&ss[i].p,&ss[i].q,&ss[i].v);
sort(ss,ss+n,cmp);
memset(dp,,sizeof(dp));
for(i=; i<n; i++)
for(j=m; j>=ss[i].q; j--)
dp[j]=maxs(dp[j],dp[j-ss[i].p]+ss[i].v);
printf("%d\n",dp[m]);
}
return ;
}

hdu 3466 Proud Merchants(有排序的01背包)的更多相关文章

  1. HDU 3466 Proud Merchants 带有限制的01背包问题

    HDU 3466 Proud Merchants 带有限制的01背包问题 题意 最近,伊萨去了一个古老的国家.在这么长的时间里,它是世界上最富有.最强大的王国.因此,即使他们的国家不再那么富有,这个国 ...

  2. Proud Merchants HDU - 3466 (思路题--有排序的01背包)

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

  3. hdu 3466 Proud Merchants 01背包变形

    Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

  4. HDU 3466 Proud Merchants【贪心 + 01背包】

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

  5. HDU 3466 Proud Merchants(01背包)

    这道题目看出背包非常easy.主要是处理背包的时候须要依照q-p排序然后进行背包. 这样保证了尽量多的利用空间. Proud Merchants Time Limit: 2000/1000 MS (J ...

  6. HDU 3466 Proud Merchants(01背包问题)

    题目链接: 传送门 Proud Merchants Time Limit: 1000MS     Memory Limit: 65536K Description Recently, iSea wen ...

  7. hdu 3466 Proud Merchants 自豪的商人(01背包,微变形)

    题意: 要买一些东西,每件东西有价格和价值,但是买得到的前提是身上的钱要比该东西价格多出一定的量,否则不卖.给出身上的钱和所有东西的3个属性,求最大总价值. 思路: 1)WA思路:与01背包差不多,d ...

  8. HDU 3466 Proud Merchants(01背包)

    题目链接: 传送门 Proud Merchants Time Limit: 1000MS     Memory Limit: 65536K Description Recently, iSea wen ...

  9. HDU 3466 Proud Merchants(背包问题,要好好理解)

    Problem Description Recently, iSea went to an ancient country. For such a long time, it was the most ...

随机推荐

  1. html 遮罩层以及弹出框的制作

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. html DOM(CSS放置位置的问题)

    转载自: http://www.php.cn/div-tutorial-386900.html (本文对读者有帮助的话请移步支持原作者) 笔记: 这样会先加载css的样式,在渲染dom的时候已经知道了 ...

  3. mac pro 1.5T内存是如何实现的

    苹果发布全新Mac Pro:28核1.5T内存 预计售价10万+ 看到这样的新闻标题是不是很震撼,甚至怀疑人生,64位机怎么就可以1.5T内存了,自己的系统盘都没那么大 而且我们知道windows下的 ...

  4. ENSP 安装后,启动路由器提示错误41

    ENSP 安装后,启动路由器提示错误41 环境: 安装的软件清单: VirtualBox-5.2.28-130011-Win.exe WinPcap_4_1_3.exe Wireshark-x64-3 ...

  5. Vue项目中出现Loading chunk {n} failed问题的解决方法

    最近有个Vue项目中会偶尔出现Loading chunk {n} failed的报错,报错来自于webpack进行code spilt之后某些bundle文件lazy loading失败.但是这个问题 ...

  6. 通用、封装、简化 webpack 配置

    通用.封装.简化 webpack 配置 现在,基本上前端的项目打包都会用上 webpack,因为 webpack 提供了无与伦比强大的功能和生态.但在创建一个项目的时候,总是免不了要配置 webpac ...

  7. SSM+Maven+IDEA增删改查

    开发工具 IntelliJ IDEA Apache-tomcat-9.0 JDK 1.8 MySQL 8.0.11 Maven 3.5.4 IDEA+Maven搭建项目骨架 1. 新建Maven项目: ...

  8. 【7.19 graphshortestpath graphallshortestpaths函数】matlab 求最短路径函数总结

    graphshortestpath 函数是用来解决最短路径问题的. 语法为: [dist, path, pred]=graphshortestpath(G,S) [dist, path, pred]= ...

  9. Spring → 02:开发初步

    一.搭建开发环境 1.1.IDE的安装和配置 1.2.开发包的准备及开发包介绍 二.Hello World 2.1.Bean的编码 2.2.Spring配置文件编写 2.3.测试与运行 三.Sprin ...

  10. node 写的简单爬虫(三)

    异步爬取数据 先引入 var async = require('async'); 然后同样上代码 var topicUrls = [];//存所有地址 http.get(url,function(re ...