Knapsack problem, because the data is not easy DP, instead of pruning, first by cost-effective sorting, if the rest of the backpack space at the highest cost-effective selection will not be better than the best solution has been found pruning, that is
if (val + (LD) Pk[d].val/(LD) PK[D].W * (lim-w) + EPS <= ans) {
Return
}
I didn't think so, 0ms.
#include <cstdio> #include <iostream> #include <cstdlib> #include <cstring> #include <string > #include <algorithm> #include <map> #include <queue> #include <vector> #include <cmath > #include <utility>using namespace std;typedef long long ll;typedef long double ld;const int N = 108, INF = 0x3f3f 3f3f, EPS = 0.4;struct data{ll W, val;bool operator< (const data &TP) Const{return val * tp.w > Tp.val * w;}} Pk[n];int N; LL Sum[n], Sw[n], Lim; ll ans;void dfs (int d, LL W, ll Val) {if (val > ans) {ans = val;} if (d >= N) {return;} if (val + (LD) Pk[d].val/(LD) PK[D].W * (lim-w) + EPS <= ans) {return;} if (w + pk[d].w <= Lim) {DFS (d + 1, W + pk[d].w, Val + pk[d].val);} DFS (D + 1, W, Val);} int main () {while (~scanf ("%d%i64d", &n, &lim)) {for (int i = 0; i < n; i++) {scanf ("%i64d%i64d", & ;p K[I].W, &pk[i].val); } sort (PK, PK + N); Ans = 0; Sum[n] = 0; Sw[n] = 0; DFS (0, 0, 0); printf ("%i64d\n", ans); } return 0;}
HDU5887 Herbs Gathering (search pruning)