Hdoj1561the more, the better (tree DP, dependent on backpack)

Source: Internet
Author: User

Question: hdoj1561the more, the better


Acboy is very fond of 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:

Category: Tree DP entry, dependent on backpack


I read about the backpack 9 again before doing this. It feels so classic, especially the general backpack, which is the essence. The relation between this question is to rely on the backpack bare, and solve it with a tree-like DP.

First, the restriction is to select m items, and each item can be selected at most once. The difference from a 0-1 backpack is that there is a dependency, so we can solve this dependency by using a tree. With DFS, start DFS from the root node, and then perform the 0-1 backpack DP during the rollback until the leaf node.

Define the status: DP [I] [J] indicates that on node I, select the maximum value of J cities from the tree where I is the root node.

Initialization: DP [I] [J] = Val [I] (value of the I node) (1 <= j <= m)

Transition equation DP [Father] [J] = max (DP [Father] [J], DP [Father] [k] + dp [child] [J-K ]); as can be seen from the DFS above, we use the child node to update the parent node, use J to enumerate the number of cities selected by the parent node, and use K to enumerate the number of cities selected by other child nodes. Then we can move it over.


Note: This question is given to many initial nodes, that is, there are many forests. We just need to set a super root node to connect the subtree.


Code:

# Include <iostream> # include <vector> # include <cstring> # include <cstdio> # include <string> # include <algorithm> # include <vector> # define del (, b) memset (a, B, sizeof (A) const int n = 150; using namespace STD; int n, m; int DP [N] [N], vis [N]; // DP [I] [J] indicates that in node I, the maximum value of J cities is selected from the subtree where I is the root node. Int CAP [N], val [N]; vector <int> V [N]; void creat (int o) {for (INT I = 0; I <V [O]. size (); I ++) {int T = V [O] [I]; If (vis [T] = 0 & V [T]. size ()> 0) creat (t); For (Int J = m; j> 1; j --) // J> 1 indicates that this node must have a 0-1 backpack {for (int K = 1; k <j; k ++) // enumerate the number of selectable city DP [O] [J] = max (DP [O] [J], DP [O] [k] + dp [T] [J-K]) ;}} int main () {While (CIN> N> m, N & M) {M ++; // Add a root node del (DP, 0); For (INT I = 1; I <= N; I ++) {int A, B; CIN> A> B; V [A]. push_back (I); For (Int J = 1; j <= m; j ++) DP [I] [J] = B; // each node during initialization, all statuses are represented by one of your creat (0); For (INT I = 0; I <= N; I ++) V [I]. clear (); cout <DP [0] [m] <Endl;} return 0 ;}




Hdoj1561the more, the better (tree DP, dependent on backpack)

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.