HihoCoder 1104 : Suzhou Adventure(树形DP)
Suzhou Adventure
描述
Little Hi is taking an adventure in Suzhou now. There are N beautiful villages in Suzhou which are numbered from 1 to N. They connected by N-1 roads in such a way that there is excactly one way to travel from one village to another. Little Hi gives each village a score according to its attractiveness. He is visiting village 1 now and plans to visit excatly M villages (including village 1) and maxize the total score of visited villages. Further more, K villages are recommended by Little Ho. He does not want to miss these recommended villages no matter what their attractiveness scores are.
Note that Little Hi visits every village on his travel route. Passing a village without visiting it is not allowed. Please find the maximum total score Little Hi can get.
输入
The first line contains 3 integers N(1 <= N <= 100), K(1 <= K <= 5) and M(1 <= M <= N), representing the number of villages, the number of recommended villages and the number of villages Little Hi plans to visit.
The second line contains N integers, the attractiveness scores of villages. The scores are between 1 to 100.
The third line contains K integers, the list of recommended villages.
The following N-1 lines each contain two integers a and b, indicating that village a and village b are connected by a road.
输出
The maximum scores Little Hi can get. If there is no solution output -1.
- 样例输入
-
5 2 4 1 2 3 4 5 3 4 1 2 1 3 1 4 2 5
- 样例输出
-
10
给出一些城市,每个城市有一个满意度,某一些城市是必须去的,且访问量受限制,求最大满意度。
方法1,预处理得到必须访问的村庄集合,以便在动态规划时使用辅助决策,根据状态转移方程求解。可以理解为把缩点:必须要去的缩为1号点。
方法2,把必须要去的城市加上一个很大的满意度,这样保证它一定会被选到。输出时再减去即可。
这道题的数据有问题,就不贴代码了。