首页 > 代码库 > 【HDOJ】1881 毕业bg

【HDOJ】1881 毕业bg

01背包。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 
 5 #define MAXN 1005
 6 
 7 typedef struct {
 8     int h, l, t;
 9 } node_st;
10 
11 node_st nodes[35];
12 
13 int dp[MAXN];
14 
15 int comp(const void *a, const void *b) {
16     node_st *p = (node_st *)a;
17     node_st *q = (node_st *)b;
18     return p->t-q->t;
19 }
20 
21 int main() {
22     int n, max;
23     int i, j, tmp;
24 
25     while (scanf("%d",&n)!=EOF && (n>=0)) {
26         max = 0;
27         for (i=1; i<=n; ++i) {
28             scanf("%d%d%d", &nodes[i].h,&nodes[i].l,&nodes[i].t);
29             if (nodes[i].t > max) max = nodes[i].t;
30         }
31         qsort(nodes+1, n, sizeof(node_st), comp);
32         memset(dp, 0, sizeof(dp));
33         for (i=1; i<=n; ++i) {
34             for (j=nodes[i].t-nodes[i].l; j>=0; --j) {
35                 tmp = dp[j] + nodes[i].h;
36                 if (dp[j+nodes[i].l] < tmp)
37                     dp[j+nodes[i].l] = tmp;
38             }
39         }
40         n = 0;
41         for (i=1; i<=max; ++i)
42             if (dp[i] > n)
43                 n = dp[i];
44         printf("%d\n", n);
45     }
46 
47     return 0;
48 }