HDU 1561 The more, The Better (tree dp + backpack)
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 6000 Accepted Submission (s): 3548
Problem Description ACboy enjoys a strategy game. There are N castles on a map, each of which has certain treasures, in each game, ACboy allows him to conquer M castles and gain the treasures in them. However, due to the geographical location, some castles cannot be directly conquered. To conquer these castles, you must first conquer another particular Castle. Can you help ACboy figure out which M castle should be conquered to obtain as many treasures as possible?
Input each test instance first contains two integers, N, M. (1 <= M <= N <= 200); In the next N rows, each row contains two integers, a, B. in line I, a represents that to conquer castle I, you must first conquer Castle a. If a = 0, you can directly conquer castle I. B indicates the number of treasures in castle I, and B> = 0. When N = 0, M = 0, the input ends.
Output outputs an integer for each test instance, which represents the maximum number of treasures ACboy has gained from conquering M castles.
Sample Input
3 20 10 20 37 42 20 10 42 17 17 62 20 0
Sample Output
513
Author 8600
Source HDU 2006-12 Programming Contest
/* Question: Chinese topic ideas: tree-like dp + backpack, which treats unlimited points as Father's Day points, after this point, go to another point dp [v] [I] to the v node and go to the value transfer equation dp [v] [I] = max (dp [v] [i], dp [v] [I-j] + dp [to] [j]); */# include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define L (x) (x <1) # define R (x) (x <1 | 1) # define MID (x, y) (x + y)> 1) # define eps 1e-8typedef _ int64 ll; using namespace std; # define INF 0x3f3f3f # define N 205int va [N], n, p; vector
G [N]; int dp [N] [N]; int vis [N]; void dfs (int x) {int I, j; vis [x] = 1; dp [x] [1] = va [x]; for (I = 0; I
0; v --) for (int j = 1; j