hdu5188限制01背包

zhx and contest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 731    Accepted Submission(s): 271


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!

题意:zhx是一个编程大神,一天来参加比赛,人人都知道他是大神,管理者就看着他,如果他在li时间之前做出第i道题,他就是大师,但是zhx不想被称为大师,但是他又想在最短时间内得到w分,每道题有一个分值,zhx做每道题要花费一定时间,每道题有一个li时间,即他在li时间内做完第i道题,他就是大师;

样例1 , 花费时间是ti=1,得分是ci=4,li=7,在第6分钟开始做,第7分钟做完,最短时间是7钟;

思路:每道题目从(li-ti)时刻开始做是最优的,每道题按照(li-ti)升序排序,dp[j]表示花费j时得到的最大分值,背包容量就是时间,最大时间(最大容量)是设为sum(ti)+max(li),即在截止时间最晚的li上加上所有题目做完花费的时间,状态转移方程 :dp[j]=max(dp[j],dp[j-max(ti,li)]+ci);

ac代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int maxn=200100;
struct ac
{
    int t,c,l;
}d[50];
bool cmp(ac a,ac b)
{
    return a.l-a.t<b.l-b.t;///按照开始时间排序
}
int dp[maxn];
int main()
{
    int n,w;
    int W,L,C;
    while(~scanf("%d%d",&n,&w))
    {
        W=0;
        L=0;
        C=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&d[i].t,&d[i].c,&d[i].l);
             W+=d[i].t;
             C+=d[i].c;
             L=max(L,d[i].l);
        }
        W+=L;
        if(C<w)
        {
            printf("zhx is naive!\n");
            continue;
        }
        sort(d,d+n,cmp);
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++)
        {
            for(int j=W;j>=max(d[i].t,d[i].l);j--)
            {
                dp[j]=max(dp[j],dp[j-d[i].t]+d[i].c);
            }
        }
        for(int j=0;j<=W;j++)
        {
            if(dp[j]>=w)
            {
                printf("%d\n",j);
                break;
            }
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值