Hdu1561 the more, the better (tree DP + 01 backpack)

Source: Internet
Author: User

Question:

Problem descriptionacboy enjoys playing a strategic game. on a map, there are n castles, 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? Analysis: compared with the preceding primary-tree DP, this topic has one more constraint, namely, selecting M nodes, which naturally associates with a backpack problem, so how do I build a 01 backpack on the tree? Do not consider the forest. For the current tree, it is the combination of the status of the tree with the current node as the root and the status of the tree with a child tree for (Int J = T; j> = 1; j --)
{
For (int K = 1; k <= now; k ++)
DP [u] [J + k] = max (DP [u] [J + K], DP [u] [J] + dp [w] [k]);
}
// I personally think T and now variables are the key to this question. T is the total number of nodes that have been traversed currently, and does not contain the tree with w as the root.
// These two variables mainly deal with the status which will affect the transfer of the current status.
// In other words, in the current state, it is the tree with the root of U. To select the castle on the tree with the root of W, the status that can appear is from
// The number of statuses T on the tree with the root of U and the number of statuses now on the tree with the root of W
// It may be a bit cumbersome to explain. DP [u] [J] indicates the tree rooted in U and selects the optimal solution of J subnodes.
# Include <iostream> # include <algorithm> # include <vector> using namespace STD; int V [201], DP [201] [201], F [201], n, m; vector <int> G [1, 201]; int DFS (int u) {DP [u] [1] = V [u]; // This indicates that the parent node will definitely select int T = 1; int size = G [u]. size (); For (INT I = 0; I <size; I ++) {int W = G [u] [I]; int now = DFS (w ); // perform in-depth search and calculate the 01 backpack for (Int J = T; j> = 1; j --) {for (int K = 1; k <= now; k ++) DP [u] [J + k] = max (DP [u] [J + K], DP [u] [J] + dp [w] [k]);} // I personally think T and now variables are the key to this question, t indicates the total number of nodes that have been traversed, Trees with W root are not included. // these two variables mainly deal with the status which will affect the transfer of the current status. // In other words, in the current state, it is the tree with u as the root. To select the castle on the tree with w as the root, then, the status that can appear is the number of States that have occurred on the tree rooted in U, T and W, and the number of States that have occurred on the tree having the root, now and the selected status. // It may be explained T + = now ;} return t;} int main () {While (scanf ("% d", & N, & M) = 2 & (M | N )) {int B; For (INT I = 0; I <= N; I ++) {f [I] = I; G [I]. clear () ;}for (INT I = 1; I <= N; I ++) {scanf ("% d", & B, & V [I]); If (B! = 0) {G [B]. push_back (I); F [I] = B;} V [0] = 0; For (INT I = 1; I <= N; I ++) // convert the forest into a tree {If (F [I] = I) {G [0]. push_back (I); // 0 node as root node} memset (DP, 0, sizeof (DP); DFS (0); printf ("% d \ n ", DP [0] [M + 1]); // because of the addition of a castle numbered 0, a total of m + 1 castles are attacked} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.