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


相关推荐

  • C语言数组练习题目

    C语言数组练习题目C语言数组练习题目1、编写程序,输入10个整数存入一维数组,统计输出其中的正数、负数和零的个数。#include<stdio.h>main(){ inta[10],i,j=0,k=0,l=0; printf(“请输入10个整数:”); for(i=0;i<10;i++) { scanf(“%d”,&a[i]); } for(i=0;i<10;i++) { if(a[i]>0) ++j; elseif(a[i]==0) ++k

    2022年7月11日
    14
  • 19号拌面[通俗易懂]

    19号拌面[通俗易懂]这几天在上地主要在一家叫19号拌面的餐厅吃饭,面条很硬,味道也一般,项目的洽谈了1天半,感觉很疲惫,昨天是12点睡的,今天还不知道是什么时候?明天必须给出2套解决方案出来,客户也很精明,让我们把所有可

    2022年7月1日
    23
  • linux ll命令无效

    linux ll命令无效1.编辑~/.bashcvim~/.bashc若vi/vim命令无效,参考bash:vi:commandnotfound/bash:vim:commandnotfound2.重新执行刚修改的初始化文件source~/.bashc

    2022年6月23日
    31
  • Apache配置反向代理

    Apache配置反向代理为了让自己的 springboot 项目能被域名直接访问 而不是 IP 端口号的形式访问 需要用到反向代理 简单来讲就是把一个程序运行的地址映射到域名上 实现直接用域名访问 网上很多教程都是针对 nignx 的 而我用的是 apache 也不想折腾把 apache 换成 nignx 找了很久才找到一个可以用的 这里记录一下 方便下次使用

    2025年8月3日
    4
  • python中的深拷贝和浅拷贝_python浅复制和深复制的区别

    python中的深拷贝和浅拷贝_python浅复制和深复制的区别薇尔莉特来了~~~~~~

    2022年9月27日
    2
  • 解决JetBrains 账户连接错误,hosts下并没有0.0.0.0 account.jetbrains.com问题

    解决JetBrains 账户连接错误,hosts下并没有0.0.0.0 account.jetbrains.com问题JetBrains帐户连接错误:连接超时:连接您的主机可能在代理的后面。PhpStorm无法检测到您的代理配置。您可能希望指定HTTPS代理参数,然后重试。代理主机:代理端口:onedetermine我的hosts下面并没有0.0.0.0 account.jetbrains.com0.0.0.0www.jetbrains.com这里修改了下我本机的DNS改为 114….

    2022年8月18日
    6

发表回复

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

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