hdu5188 加限制的01背包问题「建议收藏」

hdu5188 加限制的01背包问题

大家好,又见面了,我是全栈君。

http://acm.hdu.edu.cn/showproblem.php?

pid=5188

Problem Description
As one of the most powerful brushes in the world, zhx usually takes part in all kinds of contests.

One day, zhx takes part in an contest. He found the contest very easy for him.

There are 



n
 problems in the contest. He knows that he can solve the 



ith
 problem in 



ti
 units of time and he can get 



vi
 points.

As he is too powerful, the administrator is watching him. If he finishes the 



ith
 problem before time 



li
, he will be considered to cheat.

zhx doesn’t really want to solve all these boring problems. He only wants to get no less than 



w
 points. You are supposed to tell him the minimal time he needs to spend while not being considered to cheat, or he is not able to get enough points. 

Note that zhx can solve only one problem at the same time. And if he starts, he would keep working on it until it is solved. And then he submits his code in no time.
 


Input
Multiply test cases(less than 



50
). Seek 



EOF
 as the end of the file.

For each test, there are two integers 



n
 and 



w
 separated by a space. (



1n30




0w109
)

Then come n lines which contain three integers 



ti,vi,li
. (



1ti,li105,1vi109
)
 


Output
For each test case, output a single line indicating the answer. If zhx is able to get enough points, output the minimal time it takes. Otherwise, output a single line saying “zhx is naive!” (Do not output quotation marks).
 


Sample Input
   
   
1 3 1 4 7 3 6 4 1 8 6 8 10 1 5 2 2 7 10 4 1 10 2 3

 


Sample Output
   
   
7 8 zhx is naive!

/**
hdu5188 有限制条件的01背包问题
题目大意:有n道题i题用时ti秒,得分vi,在li时间点之前不能做出来,并且一道题不能分开几次做(一旦開始做,必须在ti时间内把它做完)
          问得到w分的最小用时是多少
解题思路:非常像01背包的基本题,可是有一个li分钟前不能AC的限制,因此第i道题必须在最早第(li-ti)时刻做,我们依照l-t递增排序,然后依照经典解法来做
          即可了
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;

struct note
{
    int t,v,l;
    bool operator <(const note &other)const
    {
        return l-t<other.l-other.t;
    }

}node[35];

int n,m;
int dp[3000005];

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int sum=0,ans=0,up=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&node[i].t,&node[i].v,&node[i].l);
            sum+=node[i].v;
            ans+=node[i].t;
            up=max(up,node[i].l);
        }
        if(m>sum)
        {
            printf("zhx is naive!\n");
            continue;
        }
        sort(node,node+n);
        up=max(up,ans);
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++)
        {
            for(int j=up;j>=node[i].l;j--)
            {
                if(j>=node[i].t)
                {
                    dp[j]=max(dp[j],dp[j-node[i].t]+node[i].v);
                }
            }
        }
        int flag=0;
        for(int i=0;i<=up;i++)
        {
            if(dp[i]>=m)
            {
                printf("%d\n",i);
                flag=1;
                break;
            }
        }
        if(flag==0)
        {
            printf("zhx is naive!\n");
        }
    }
    return 0;
}

Problem Description
As one of the most powerful brushes in the world, zhx usually takes part in all kinds of contests.

One day, zhx takes part in an contest. He found the contest very easy for him.

There are 



n
 problems in the contest. He knows that he can solve the 



ith
 problem in 



ti
 units of time and he can get 



vi
 points.

As he is too powerful, the administrator is watching him. If he finishes the 



ith
 problem before time 



li
, he will be considered to cheat.

zhx doesn’t really want to solve all these boring problems. He only wants to get no less than 



w
 points. You are supposed to tell him the minimal time he needs to spend while not being considered to cheat, or he is not able to get enough points. 

Note that zhx can solve only one problem at the same time. And if he starts, he would keep working on it until it is solved. And then he submits his code in no time.
 


Input
Multiply test cases(less than 



50
). Seek 



EOF
 as the end of the file.

For each test, there are two integers 



n
 and 



w
 separated by a space. (



1n30




0w109
)

Then come n lines which contain three integers 



ti,vi,li
. (



1ti,li105,1vi109
)
 


Output
For each test case, output a single line indicating the answer. If zhx is able to get enough points, output the minimal time it takes. Otherwise, output a single line saying “zhx is naive!” (Do not output quotation marks).
 


Sample Input
    
    
1 3 1 4 7 3 6 4 1 8 6 8 10 1 5 2 2 7 10 4 1 10 2 3

 


Sample Output
    
    
7 8 zhx is naive!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116013.html原文链接:https://javaforall.net

(0)
上一篇 2022年1月27日 上午11:00
下一篇 2022年1月27日 上午11:00


相关推荐

  • Coturn配置

    Coturn配置原文链接 http blog csdn net u0 article details coturn 服务器下载 https github com coturn coturn 由三个地方需要修改 nbsp 1 vim etc default coturn 把上面打开编辑的文件中的这一行 TURNSERVER ENABLED 1 去掉

    2026年3月20日
    2
  • 深度学习(五)学习率的调节

    深度学习(五)学习率的调节   学习率对于深度学习是一个重要的超参数,它控制着基于损失梯度调整神经网络权值的速度,大多数优化算法(SGD、RMSprop、Adam)对其都有所涉及。学习率越小,损失梯度下降的速度越慢,收敛的时间更长,如公式所示:new_weight=existing_weight—learning_rate*gradient(新权值=当前权值–学习率×梯度)    如果学习…

    2022年5月20日
    87
  • latex 公式换行的命令

    latex 公式换行的命令2019独角兽企业重金招聘Python工程师标准>>>…

    2022年6月10日
    41
  • 走近代码之Python–爬虫框架Portia | 艾伯特

    走近代码之Python–爬虫框架Portia | 艾伯特Portia nbsp 基于 Scrapy 的可视化数据采集框架走近代码之 Python 爬虫框架 Scrapy1 框架特性基于 scrapy 内核可视化爬取内容 不需要任何开发专业知识动态匹配相同模板的内容 2 安装 Windows 推荐使用 Docker 安装安装 DockerToolBo 启动 nbsp dockerrun v F pywp portia app data projects

    2026年3月18日
    3
  • MATLAB快速搭建一个神经网络以及神经网络工具箱的使用

    MATLAB快速搭建一个神经网络以及神经网络工具箱的使用文章目录0.导读1.神经网络工具箱2.如何利用MATLAB工具箱建立神经网络人工神经网络学习笔记2——MATLAB神经网络工具箱神经网络工具箱的使用MATLAB中神经网络工具箱的使用0.导读首先声明,这篇文章的内容并不全是本人的原创内容,凡是引用了别人的博客或者文章的地方,我都会标注出来,以便大家阅读原文。现在最前面的,当然是提纲挈领的废话。凡是商品都有目标人群,文章也该如此,一篇文章写了…

    2022年6月20日
    114
  • java正则表达式解析「建议收藏」

    java正则表达式解析「建议收藏」“正则表达式”到用时方恨少!学习正则表达式,我觉得还是要循循渐进,由易到难,一点点深入……(本人也在学习中这里提供个人理解思路,以及一些大神们的独到讲解。。。。。。)一、知道java正则表达式是干什么的?百度百科定义:其实这已经说得很明确了,正则表达式其实就是一个字符串,这个字符串是按照一定的规则进行组合得来的,而这个规则当然是创始者定义,用这些规则我们能做什么呢?看红…

    2022年7月19日
    21

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号