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


相关推荐

  • Java面试题

    Java面试题Java面试题

    2022年4月23日
    38
  • CheckSum 计算工具1——bin文件

    CheckSum 计算工具1——bin文件

    2021年8月27日
    607
  • 数据接口-免费版(股票数据API)「建议收藏」

    获取股票数据的源头主要有:数据超市、雅虎、新浪、Google、和讯、搜狐、ChinaStockWebService、东方财富客户端、证券之星、网易财经。数据超市2016年5月6日更新。根据最近频繁出现的数据超市,可以无限制获取相关数据,而不再需要使用爬虫等方式获取,这样不仅节省了极大资源,也有利于遍历数据。具体的方法不再赘述,列出来相关网站清单,开发者可自行到这些网站查询调用方法。聚合数据 htt…

    2022年4月7日
    74
  • 振动分析软件有哪些_excel控件按钮怎么控制图表

    振动分析软件有哪些_excel控件按钮怎么控制图表LightningChart是优化了GPU加速,硬件性能的制图组件,用于实时呈现超过10亿个数据点的海量数据。同时LightningChart是为了处理实时数据采集和处理而开发的,可有效利用CPU和内存资源。LightningChart包括广泛的2D,高级3D,Polar,Smith,3D饼/甜甜圈,地理地图和GIS图表以及适用于科学,工程,医学,航空,贸易,能源和其他领域的体绘制功能。当您想到振动分析时,您会想到什么?它正在成为结构工程中一种非常常见的识别方法,用于识别潜在的结构完整性问题,例如隐藏的物

    2022年10月15日
    0
  • 【JMeter】参数Parameters和Body Data

    【JMeter】参数Parameters和Body Data在做接口并发测试的时候,才发现Jmeter中的Parameters和BodyData两种参数格式并不是简单的一个是xx=xx,另外一个是json格式的参数先看一个接口[post]/api/xx/xxxx/xxxx通知服务端文件上传完毕输入参数:httpcontenttype:application/json名称|类型|是否必须|参数限制|描述———|–

    2022年6月23日
    18
  • C# 之 System.Object

    C# 之 System.Object

    2021年11月29日
    36

发表回复

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

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