HDU-5188-zhx and contest【01背包】

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!

题目链接:HDU-5188

题目大意:给出n道题目,每道题目给出t,v,l。每道题目需要t时间去完成,必须在l及l时间之后完成。v是完成这道题目获得的分值。问,需要获得大于等于w分,最快什么时候能完成。

题目思路:直接01背包就行了。。但是需要注意的是,将书序排序的时候按照

排序:按照起始时间排,即l - t

以下是代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

const int maxn = 100005;
struct node{
    int t,v,l;
}p[maxn];
int dp[maxn];
bool cmp(node a, node b)
{
    return a.l - a.t < b.l - b.t;
}
int main()
{
    int n,w;
    while(cin >> n >> w)
    {
        int maxt = 0, maxl = 0;
        memset(dp, 0, sizeof(dp));
        for (int i = 0; i < n; i++)
        {
            cin >> p[i].t >> p[i].v >> p[i].l;
            maxt += p[i].t;
            maxl = max(maxl, p[i].l);
        }
        maxt = max(maxt, maxl);
        sort(p, p + n, cmp);
        int ans = -1;
        for (int i = 0; i < n; i++)
        {
            for (int j = maxt; j >= p[i].l; j--)
            {
                if (j < p[i].t) continue;

                dp[j] = max(dp[j], dp[j - p[i].t] + p[i].v);
            }
        }
        for (int i = 0; i <= maxt; i++)
        {
            if (dp[i] >= w)
            {
                ans = i;
                break;
            }
        }
        if (ans == -1) cout << "zhx is naive!\n";
        else cout << ans << endl;
    }
    return 0;
}

你可能感兴趣的:(HDU,ACM_动态规划)