tree dp

本文探讨树型DP的概念,通过与一般DP的对比,阐述其特点和应用。介绍如何利用树的遍历实现DP,并通过三个例题,包括电影票购买问题,展示树型DP的具体解题思路和优化策略。
摘要由CSDN通过智能技术生成

浅谈树型DP(提纲)

 

--sanpin


说两道很相似的题目,借此谈谈对树型DP的理解。


DP与其它之间

DP是一种思想,在很多算法的实现中都会用到,绝大多数算法都能与之扯上关系。这一点,可以参看IOI2000集训队论文《 动态规划的特点及其应用》(张辰)



树型DP的一种体现与一种实现:

细看来,图的遍历蕴含了树型DP的想法,或者反过来说:树型DP可籍由遍历来实施。



树型DP与一般的DP:

树型DP的特殊之处在于其实施的数据结构,树(有根或无)或图。


DP的精髓在记录,关键在状态转移.


树型DP的一个特点是大部分题目的状态转移关系很明显,从这一点来说,比起一般的DP要简单一些;但同时其编码复杂度可能会高一些,这与其数据结构有关。




具体方法:

一般情况下,把树看成有根树,从下到上,以每一棵子树为阶段;


考虑在整个树所形成的一个“方案”中,每一棵子树有哪些状态;

把这些状态用函数表示出来,找到状态与状态之间的转移关系。




例题:







题目一:

描述:

Let's Go to the Movies

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 N3Nk

where N1 is the name of a parent, with N2Nk 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

Arab and North Africa 2007

很明显,满足最优性,是树型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

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值