This topic really opened the eye, very strong, just started directly to the Q sort, direct WA, it seems that the answer to God's cattle after understanding to follow the q-p to arrange, think carefully, consider the special situation if the time is ordinary 01 backpack, according to Q row is definitely a problem, But according to Q-p is actually in the array with the new process in the number of affected States, the larger the q-p, then the number of the influence of the later state, but the specific mathematical proof really does not, just with a special example of their own to deduce a few times ...
Title Link: https://vjudge.net/problem/HDU-3466
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 505;
int cntn, CNTM;
int dp[5005];
struct Node {
int p, q, v;
Node (int p=0, int q=0, int v=0): P (P), q (q), V (v) {}
bool operator < (const node& RHS) Const {
return q-p < RHS.Q-RHS.P;
}
} NODE[MAXN];
void Solve () {for
(int i = 1; I <= cntn; i++) {for
(int j = Cntm; J >= node[i].q; j--)
dp[j] = Max (Dp[j], dp[j-node[i].p]+node[i].v);
}
printf ("%d\n", Dp[cntm]);
}
int main () {while
(scanf ("%d%d", &CNTN, &CNTM)! = EOF) {
memset (dp,0,sizeof (DP));
memset (node,0,sizeof (node));
for (int i = 1; I <= cntn; i++)
scanf ("%d%d%d", &NODE[I].P, &NODE[I].Q, &NODE[I].V);
Sort (node+1, node+cntn+1);
Solve ();
}
}