poj_2486 Dynamic Planning

Source: Internet
Author: User

Main Topic

n nodes form a tree, each node has a weight of val[i], from the root node root on the tree to walk, walking only along the branches of the road. Up to the tree on the K-step, every first time to reach a node J, you can get the proceeds of Val[j], to start from the root, up to the K-step, you can get the maximum benefit.

Problem Analysis

     tree structure + optimization problem, consider using dynamic programming to solve, tree-shaped dynamic programming DP state, the first dimension dp[i][...] Generally refers to the XXXX from the I node or the I node as the root.  
/*dp[i][j][0] indicates that from node I, walk J, finally back to the I node, the maximum benefit can be obtained  
Dp[i][j][1] indicates that from node I, Walk J step, ultimately do not return to the I node, the maximum benefit can be obtained  
obviously:  
(1) Starting from I go j step back to I get the maximum gain vs. the child node of enumeration I, son, from I go j-k step back to I (which does not include son node), and then  
Add k-2 step from son, go back to son node, plus I-->son and son-->i two steps  
Dp[i][j][0] = max (Dp[i][j][0], dp[i][j-k][0] + dp[son][k-2][0] + Val[son])

(2) Starting from I go J step does not return to I get maximum gain vs. from I go j-k step back to I (which does not include son node), and then
Plus k-1 step from son, don't go back to son node, plus I-->son step
DP[I][J][1] = max (dp[i][j][1], dp[i][j-k][0] + dp[son][k-1][1] +val[son]);

(3) Starting from I go J step does not return to I get maximum gain vs. from I go j-k step not back I (which does not include son node), and then
Plus take k-2 step from son, back to son node, plus I-->son and son-->i two steps
DP[I][J][1] = max (dp[i][j][1], dp[i][j-k][1] + dp[son][k-2][0] + Val[son]);

Take dp[i][j][1] = max (dp[i][j][1], dp[i][j-k][1] + dp[son][k-2][0] + Val[son]); For example, if you need to consider son node walk k-2 step
And from son's parent node I begin to walk j-k step coincident? The answer is no, because in order of the child nodes of parent node I, we enumerate the son node in turn, and when enumerating to the current son node,
DP[I][J-K][1] does not contain the current son's contribution
*/

Implementation (c + +)
#include <stdio.h> #include <string.h> #define Max_node_num 205#define max_step_num 205#define MAX (A, B) a >b? A:b/*dp[i][j][0] means starting from node I, walk J, and finally back to the I node, the maximum benefit can be obtained dp[i][j][1] from the node I, Walk J step, ultimately do not return to the I node, the maximum benefit can be obtained is: (1) Starting from I go j step back to I get the maximum gain vs. the child of enumeration I, son, from I go j-k step back to I (which does not include son node), plus k-2 step from son, go back to son node, plus I-->son and son-->i Two-step dp[i][j][0] = max (dp[i][j][0], dp[i][j-k][0] + dp[son][k-2][0] + Val[son]) (2) Starting from I go J step do not return to I get maximum gain vs. departure from I J-k Step back to I (which does not include son node), plus k-1 step from son, do not go back to son node, plus i-->son step dp[i][j][1] = max (dp[i][j][1], dp[i][j-k][0] + dp[son][ K-1][1] +val[son]); (3) Starting from I go J step does not go back to I get maximum gain vs. from I go j-k step does not go back to I (which does not include son node), plus walk from son k-2 step, back to son node, plus I-->son and son-- >i two-step dp[i][j][1] = max (dp[i][j][1], dp[i][j-k][1] + dp[son][k-2][0] + Val[son]); dp[i][j][1] = max (dp[i][j][1], dp[i] [J-k] [1] + dp[son][k-2][0] + Val[son]); For example, is it necessary to consider the son node walking k-2 step and starting from son's parent node I walk j-k step coincident? The answer is no, because the son node is enumerated in the order of the child nodes of parent node I, and in the enumeration to the current son node, dp[i][j-k][1] does not contain the current son's contribution */int dp[max_node_num][max_step_num][2] ; int Val[max_node_num];int N, k;struct edge{int v;//v represents the child node to which the edge points int Next;//next is the index edge of the sibling edge of the edge (int vv =-1, int nn =-1): V (VV), next (NN) {}};  Edge Gedges[2*max_node_num]; Constructing multi-fork-tree int gedgeindex;int Ghead[max_node_num] with static array; Ghead[i] represents the rightmost child node of node I, initially -1void insertedge (int u, int v) {//because it is not possible to determine which of you and V is the parent node in the final tree which is the child node//so that you and V are the parent and child nodes, respectively. Forming Edge gedges[gedgeindex].v = V;gedges[gedgeindex].next = Ghead[u];ghead[u] = gedgeindex++;} void Init (int n, int k) {for (int i = 1; I <= n; i++) {Ghead[i] = -1;for (int j = 1; j <= K; j + +) {Dp[i][j][0] = dp[i][ J][1] = 0;}} Gedgeindex = 0;} void Dfs (int u, int father) {for (int i = ghead[u]; I! =-1; i = gedges[i].next) {int v = gedges[i].v;if (v = = father)//for two Nodes U and V, with U-->v side, also have v-->u side, in order to avoid the dead loop Continue;dfs (V, U); for (int j = k; j >= 0;--j) {for (int t = 1; j + t <= K; + +) T) {dp[u][j + t][0] = max (dp[u][j + t][0], dp[u][j][1] + dp[v][t-1][0] + val[v]); if (T >= 2) Dp[u][j + t][0] = max (dp[u ][j + t][0], dp[u][j][0] + dp[v][t-2][1] + val[v]); if (T >= 2) Dp[u][j + t][1] = max (dp[u][j + t][1], dp[u][j][1] + dp[v][t-2][1] + val[v]);}}} int main () {int u, v;while (scanf ("%d%d", &n, &k)! = EOF) {Init (n, k); for (int i = 1; I <= n; i++) scanf ("%d", &A Mp;val[i]); for (int i = 1; i < n; i++) {scanf ("%d%d", &u, &v), Insertedge (U, v); Insertedge (v, u);} DFS (1,-1);p rintf ("%d\n", dp[1][k][0] + val[1]);} return 0;}

poj_2486 Dynamic Planning

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.