浅谈树型DP(提纲)
--sanpin
说两道很相似的题目,借此谈谈对树型DP的理解。
DP与其它之间
DP是一种思想,在很多算法的实现中都会用到,绝大多数算法都能与之扯上关系。这一点,可以参看IOI2000集训队论文《 动态规划的特点及其应用》(张辰)
树型DP的一种体现与一种实现:
细看来,图的遍历蕴含了树型DP的想法,或者反过来说:树型DP可籍由遍历来实施。
树型DP与一般的DP:
树型DP的特殊之处在于其实施的数据结构,树(有根或无)或图。
DP的精髓在记录,关键在状态转移.
树型DP的一个特点是大部分题目的状态转移关系很明显,从这一点来说,比起一般的DP要简单一些;但同时其编码复杂度可能会高一些,这与其数据结构有关。
具体方法:
一般情况下,把树看成有根树,从下到上,以每一棵子树为阶段;
考虑在整个树所形成的一个“方案”中,每一棵子树有哪些状态;
把这些状态用函数表示出来,找到状态与状态之间的转移关系。
例题:
题目一:
描述:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 229 | Accepted: 57 |
Description
A favorite pastime for big families in Acmestan is going to the movies. It is quite common to see a number of these multi-generation families going together to watch a movie. Movie theaters in Acmestan have two types of tickets: A single ticket is for exactly one person while a family ticket allows a parent and their children to enter the theater. Needless to say, a family ticket is always priced higher than a single ticket, sometimes as high as five times the price of a single ticket.
It is quite challenging for families to decide which ticket arrangement is most economical to buy. For example, the family depicted in the figure on the right has four ticket arrangements to choose from: Seven single tickets; Two family tickets; One family ticket (for adam, bob, cindy) plus four single tickets for the rest; Or, one family ticket (for bob and his four children) plus single tickets for the remaining two.
Write a program to determine which ticket arrangement has the least price. If there are more than one such arrangement, print the arrangement that has the least number of tickets.
Input
Your program will be tested on one or more test cases. The first line of each test case includes two positive integers (S and F) where S is the price of a single ticket and F is the price of a family ticket. The remaining lines of the test case are either the name of a person going by him/herself, or of the form:
N1 N2 N3 … Nk
where N1 is the name of a parent, with N2… Nk being his/her children. Names are all lower-case letters, and no longer than 1000 characters. No parent will be taking more than 1000 of their children to the movies :-). Names are unique, the name of a particular person will appear at most twice: Once as a parent, and once as a child. There will be at least one person and at most 100,000 people in any test case.
The end of a test case is identified by the beginning of the following test case (a line made of two integers.) The end of the last test case is identified by two zeros.
Output
For each test case, write the result using the following format:
k. NS NF T
Where k is the test case number (starting at 1), NS is the number of single tickets, NF is the number of family tickets, and T is the total cost of tickets.
Sample Input
1 3 adam bob cindy bob dima edie fairuz gary 1 2 john paul george ringo 1 3 a b c 0 0
Sample Output
1. 2 1 5 2. 4 0 4 3. 0 1 3
Source
很明显,满足最优性,是树型DP。
结点状态:子树(含自身)买了各类票多少,花了几¥。
决策:
family ticket or single ticket or no ticket
转移:
if parent buy family ticket, son buy any kind, including no ticket. if parent buy single or no ticket, son buy family or single ticket.
参考代码:
太长了,不贴了,请参考电子版。
题目二:
http://acm.pku.edu.cn/JudgeOnline/problem?id=3370 Halloween treats
与上题极类似,请自行分析。
题目三:
http://acm.pku.edu.cn/JudgeOnline/problem?id=1145 Tree Summing
树型结构,但不是DP。