HDU 4341 gold miner (group backpack)
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4341
Question:
A person grabs gold at the origin (0, 0). Each piece of gold is a point (x, y) in a two-dimensional coordinate plane and Y> 0. Each piece of gold has a value V and it takes time to obtain T. If multiple gold pieces are in a straight line from the origin, they can only be captured first, and then far. Find the maximum value that can be obtained at a given time t.
Analysis:
First, think about what if all vertices are not collocated? That is, to obtain the greatest value and point within a given time t, and all points can be randomly selected. this is a 01 backpack problem. however, if a collocated point exists, how can this problem be solved?
We divide all vertices in a straight line (from the origin) into one group, and each group can only select one point. for example, if you select the point far from I in this group, you must set 1st to 2nd .. the I-1 and I point are selected. therefore, the cost and value of each group of I points are the sum of all previous points.
Now the problem becomes the problem of grouping A backpack: For each given group, only one item can be selected in the group, and you can obtain the maximum value and.
We set DP [I] [J] = x to indicate that only the items in the group I are selected. The maximum value obtained when the total cost is <= J = x.
Initialization: DP is all 0.
State Transfer: DP [I] [J] = max (DP [I-1] [J], DP [I-1] [J-cost [k] + val [k]) cost [k] and Val [k] are the costs and values of the K items in group I.
Final requirement: DP [N] [T].
Note that the three-layer loop implemented by the program cannot be interchangeable, because the above state transition equation must be implemented to guarantee the rolling array.
AC code:
# Include <cstdio> # include <algorithm> # include <cstring> # include <iostream> using namespace STD; const int maxn = 200 + 5; struct point {int X, Y; int cost, Val; bool operator <(const point & RHs) const // polar sorting {return x * RHS. y-y * RHS. x <0 | (x * RHS. y-y * RHS. X = 0 & Y <RHS. y);} bool operator = (const point & RHs) const // returns the {return x * RHS. y-y * RHS. X = 0 ;}} P [maxn]; int N, T; int N; // Number of int num [maxn]; // num [I] indicates the number of items in group I int cost [maxn] [maxn]; // cost [I] [J] = X indicates the cost of item j in group I. Int Val [maxn] [maxn]; // cost [I] [J] = X indicates the value of item j in group I. Int DP [40000 + 5]; int main () {int N, kase = 0; while (scanf ("% d", & N, & T) = 2) {// read the input for (INT I = 0; I <n; I ++) scanf ("% d", & P [I]. x, & P [I]. y, & P [I]. cost, & P [I]. val); sort (p, p + n); // the original item is divided into N groups, with only one n = 1 in each group; num [N] = 1; cost [N] [num [N] = P [0]. cost; Val [N] [num [N] = P [0]. val; For (INT I = 1; I <n; I ++) {If (P [I] = P [I-1]) // collinearity {num [N] ++; cost [N] [num [N] = cost [N] [num [N]-1] + P [I]. cost; Val [N] [num [N] = Val [N] [num [N]-1] + P [I]. val;} else // nonlinear {n ++; num [N] = 1; cost [N] [num [N] = P [I]. cost; Val [N] [num [N] = P [I]. val ;}// memset (DP, 0, sizeof (DP); For (INT I = 1; I <= N; I ++) for (Int J = T; j> = 0; j --) for (int K = 1; k <= num [I]; k ++) if (j> = cost [I] [k]) DP [J] = max (DP [J], DP [J-cost [I] [k] + val [I] [k]); printf ("case % d: % d \ n", ++ Kase, DP [T]);} return 0 ;}
HDU 4341 gold miner (group backpack)