HDU5188(有限制的01背包)

zhx and contest
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!

有限制的01背包,在某个时间之前该项不允许完成。所以就想到让耗时和限制时间相差最少的先选,这样可以使得时限尽量小的影响工作的选择。然后就背包一下。
参考别人博客的思路照着敲下来的。比较少做dp做个mark

#include "cstring"
#include "cstdio"
#include "iostream"
#include "string.h"
#include "algorithm"
using namespace std;
typedef struct node
{
    int t,v,l;
    bool operator < ( const node& a ) const
    {
        return l-t < a.l - a.t;
    }
}node;
int dp[3000007];
int main()
{
    int n,w;
    while(~scanf("%d%d",&n,&w))
    {
        node p[31];
        memset(dp,0,sizeof(dp));
        int maxium,sum1,sum2;
        maxium=sum1=sum2=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d",&p[i].t,&p[i].v,&p[i].l);
            sum1+=p[i].v;
            sum2+=p[i].t;
            maxium=max(maxium,p[i].l);
        }
        bool flag=1;
        sort(p+1,p+n+1);
        if(sum1<w)
            flag=0;
        maxium=max(maxium,sum2);
        for(int i=1;i<=n&&flag==1;i++)
        {
            for(int j=maxium;j>=p[i].l;j--)
            {
                if(j>=p[i].t)
                    dp[j]=max(dp[j],dp[j-p[i].t]+p[i].v);
            }
        }
        for(int i=0;i<=maxium&&flag;i++)
            if(dp[i]> w)
            {
                printf("%d\n",i);
                break;
            }
        if(!flag)puts("zhx is naive!");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值