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. (



1≤n≤30
, 



0≤w≤109
)

Then come n lines which contain three integers 



ti,vi,li
. (



1≤ti,li≤105,1≤vi≤109
)
 


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. (



1≤n≤30
, 



0≤w≤109
)

Then come n lines which contain three integers 



ti,vi,li
. (



1≤ti,li≤105,1≤vi≤109
)
 


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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Linux09:(1.5k)文件查找,文件的打包与压缩「建议收藏」

    Linux09:(1.5k)文件查找,文件的打包与压缩「建议收藏」文章目录一.文件查找1.which——命令查找2.find——针对文件名,类型属性查找3.locate——依赖数据库查找二.文件的打包与压缩tar1.打包2.解压缩一.文件查找1.which——命令查找#which 命令(也可用whereis)——可获得该命令的绝对路径alias是别名,每次敲ls就相当于敲了ls –color=auto,所以ls列出来的文件会自动上色2.find——针对文件名,类型属性查找#find 路径 选项 描述eg.按名字查找/etc目录下的hosts文件#fi

    2022年8月9日
  • 成员函数

    成员函数在C++中,允许在结构体中定义函数,该函数称为“成员函数”。描述形式如下:struct结构名{数据成员成员函数};例题:身高问题输入n个学生的信息,每个学生的信息包括姓名、身高、学号。变

    2022年7月3日
  • 一个对文本内容进行排序的小程序的核心代码

    一个对文本内容进行排序的小程序的核心代码

    2022年3月12日
  • moxa串口服务器5430说明书(moxa多串口卡驱动安装)

    简介*多可让4个RS232串口设备立即联网NPort5400系列的基本功能是把您现有的串口设备联让您可以轻松方便的将串口设备连接到以太网络。不但保证您现有的硬件资源,更保证您未来的网络扩充的可能性。另外,NPort5400系列可以轻松的在串口和以太网络之间进行双向的资料传输,让您可以同时达到集中管理串口设备,和在网络中分散管理主机的目的。简单易用的串口设备联网服务器内建人性化的LCM显示屏,可…

    2022年4月13日
  • 微信小程序错误码参考大全

    微信小程序错误码参考大全

    2021年11月8日
  • Linux下安装Tomcat 10

    Linux下安装Tomcat 10(Linux)Deepin下安装Tomcat101)在官网下载tar.gz包2)解压到目录(这里‘用户名’换成你自己的)sudotar-zxvf/home/用户名/Downloads/apache-tomcat-10.0.0.tar.gz-C/usr/local3)重命名sudomv/usr/local/apache-tomcat-10.0.0/usr/local/Tomcat4)测试sudo/usr/local/Tomcat/bin/startup.sh若失败则

    2022年6月2日

发表回复

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

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