[Swust OJ 402]--Palace keeper (tree-shaped DP)

Source: Internet
Author: User

Title Link: http://acm.swust.edu.cn/problem/402/

Time limit (ms): Memory Limit (KB): 65535 DescriptionAfter the Taiping Hearts incident, Clan became the Emperor distinguished a quality bodyguard.
The palace begins with the Meridian Gate, until the harem concubines's chambers, in the shape of a tree, and some palaces can be seen in each other. The fortified, three-step, five-step, each palace to be guarded around the clock, in different palaces to arrange the cost of the guards are different.
But clan's money is not enough, in any case can not be placed in every palace of the guards.

Programming tasks: Help clan decorate the guards, under the premise of guarding all palaces, make the cost of the least money. Input
The data in the input file represents a tree, which is described as follows:
Line 1th N, which represents the number of nodes in the tree.
Line 2nd to n+1, each row describes each palace node information, in order: the Palace node marking I (0 < i<=n), in the palace to place a bodyguard of the funds required K, the side of the son number M, the next m number, respectively, the node's M-son of the label R1,r2,...,rm.
For a tree of n (0 < n <= 1500) nodes, the nodes are labeled between 1 and N, and the labels are not duplicated. OutputThe output file contains only one number, which is the minimum amount of money to be asked. Sample Input
61 30 3 2 3 42 16 2 5 63 5 04 4 05 11 06 5 0

Sample Output
25

Problem Solving Ideas:

A tree-type DP problem, respectively, using dp[i][0] to show I-point guard, dp[i][1] means I point not put guard I point by son surveillance, dp[i][2] means I point not put guard I point by the Father node monitoring three cases of the minimum cost.
(1) dp[i][0] = dp[t][0 of all child nodes T], dp[t][1], dp[t][2] the smallest of and + Vi[i] (min (dp[t][0], min (dp[t][1], dp[t][2])) (+vi[i])
(2) dp[i][1] = A child node is placed on the Guard + other nodes of the dp[t][0], dp[t][1] the smallest and
(3) dp[i][2] = dp[t][1 of all child nodes)

Notice that a long long,int will explode.

The code is as follows:
1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cstring>5 using namespacestd;6 #defineMAXN 15107 #defineINF 0x3f3f3f3f8typedefLong LongLL;9 Ten intn, x, Root, Vis[maxn], SON[MAXN][MAXN], CNT[MAXN], VI[MAXN]; OneLL TMP[MAXN], dp[maxn][3]; A //Dp[i][0] I point guard, dp[i][1] I point not put guard I point by son surveillance, dp[i][2] I point not put guard I point by parent node Monitoring three cases of the minimum cost - voidTREE_DP (intx) { -     if(dp[x][0])return; the      for(inti =1; I <= cnt[x]; i++){ -         intt =Son[x][i]; - TREE_DP (t); -dp[x][0] + = min (dp[t][0], Min (dp[t][1], dp[t][2])); +dp[x][2] + = dp[t][1]; -     } +dp[x][0] +=Vi[x]; Amemset (TMP,0,sizeof(TMP)); atLL ptr =0; -      for(inti =1; I <= cnt[x]; i++){ -         intt =Son[x][i]; -Tmp[i] = min (dp[t][0], dp[t][1]); -PTR + =Tmp[i]; -     } indp[x][1] =inf; -      for(inti =1; I <= cnt[x]; i++){ to         intt =Son[x][i]; +         if(Ptr-tmp[i] + dp[t][0] < dp[x][1]) dp[x][1] = Ptr-tmp[i] + dp[t][0]; -     } the } *  $ intMain () {Panax Notoginseng     //freopen ("402-Palace keeper. In", "R", stdin); -     //freopen ("402-Palace keeper. Out", "w", stdout); the      while(~SCANF ("%d", &N)) { +Memset (DP,0,sizeof(DP)); A          for(inti =0; I < n; i++){ thescanf"%d", &x); +scanf"%d%d", &vi[x], &cnt[x]); -              for(intj =1; J <= Cnt[x]; J + +){ $scanf"%d", &son[x][j]); $VIS[SON[X][J]] =1; -             } -         } the          for(inti =1; I <= N; i++) -         if(!vis[i]) {root = i; Break; }Wuyi TREE_DP (root); theprintf"%lld\n", Min (dp[root][0], dp[root][1])); -     } Wu     return 0; -}
View Code

[Swust OJ 402]--Palace keeper (tree-shaped DP)

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.