HihoCoder 1104 : Suzhou Adventure(树形DP)

Suzhou Adventure

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

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,把必须要去的城市加上一个很大的满意度,这样保证它一定会被选到。输出时再减去即可。

时间: 2024-11-10 11:42:42

HihoCoder 1104 : Suzhou Adventure(树形DP)的相关文章

hihoCoder #1104 Suzhou Adventure

传送门:http://hihocoder.com/problemset/problem/1104 解题思路: 这是一道树上的动态规划问题 首先将多叉树转化为二叉树存储,再根据题中推荐访问的村庄地点预先处理得到必须访问的村庄集合,以便在动态规划时使用辅助决策,根据状态转移方程求解. 1 #include <iostream> 2 #include <vector> 3 #include <cstring> 4 #include <set> 5 6 int sc

hihoCoder #1055 : 刷油漆 [ 树形dp ]

传送门 结果:Accepted     提交时间:2015-05-11 10:36:08 #1055 : 刷油漆 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho有着一棵灰常好玩的树玩具!这棵树玩具是由N个小球和N-1根木棍拼凑而成,这N个小球都被小Ho标上了不同的数字,并且这些数字都是处于1..N的范围之内,每根木棍都连接着两个不同的小球,并且保证任意两个小球间都不存在两条不同的路径可以互相到达.没错,这次说的还是这棵树玩具的故事! 小Ho的树玩具

HihoCoder 1055 : 刷油漆 树形DP第一题

刷油漆 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho有着一棵灰常好玩的树玩具!这棵树玩具是由N个小球和N-1根木棍拼凑而成,这N个小球都被小Ho标上了不同的数字,并且这些数字都是处于1..N的范围之内,每根木棍都连接着两个不同的小球,并且保证任意两个小球间都不存在两条不同的路径可以互相到达.没错,这次说的还是这棵树玩具的故事! 小Ho的树玩具的质量似乎不是很好,短短玩了几个星期,便掉漆了! "简直是一场噩梦!"小Ho拿着树玩具眼含热泪道

hiho一下第76周《Suzhou Adventure》

我是菜鸡,我是菜鸡,我是菜鸡....重要的事说三遍 算是第一次做树形dp的题吧,不太难.. 园林构成一棵树,root为1,Hi从root出发,有k个园林必须玩,每个园林游玩后会得到权值w[i],最多玩M个园林. 经过的园林必须玩,问可得到的最大权值和. 题目链接:http://hihocoder.com/problemset/problem/1104 经典的树形dp,dp[i][j],表示以i为根的子树选j个结点可得的最大权值和. 这样dp[root][num] = g[son_num][num

Suzhou Adventure,hihocoder 1104

时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 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 on

Hihocoder 1035 [树形dp]

/* 题意: 不要低头,不要放弃,不要气馁,不要慌张. PS:人生第一道自己独立做出来的树形dp... 给一棵树,标号1到n,每条边有两个权值,步行时间和驾车时间.车在1号点. 给m个必须访问的关键点,求从1号点出发,访问所有关键点一遍的最小时间. 注意车可以停在任意地方,但是只有1号点有一辆车,人最后也可以停留在任意点. 思路: 1.子树方向(注意dp1 dp2 dp4都是保证人一定要返回该点的最优解) dp1代表该点起始有车,并且从该点出发访问完该点子树上所有的关键点车和人都返回的该点的最优

UVALive 4015 - Caves(树形DP)

Description It is said that the people of Menggol lived in caves. A tribe's caves were connected to each other with paths. The paths were so designed that there was one and only one path to each cave. So the caves and the paths formed a tree. There w

树形DP新识

HihoCoder: 1041(点) 1063(边) 1035(边) 目前我遇到的树形DP有两类: ?:点处理,大概就是点的乱搞,比如找一些点,这些点权值和最大.dp[node][m],表示给node为节点的子树分配m个点的最优结果 ?:边处理,路径问题,由于'一笔画'或者其他要求,要求一条边一条边的走,需要加一维表示是否回到起点.dp[node][m][state],state表示是否回来等等.

HDU-2196 Computer (树形DP)

最近在看树形DP,这题应该是树形DP的经典题了,写完以后还是有点感觉的.之后看了discuss可以用树分治来做,以后再试一试. 题目大意 找到带权树上离每个点的最远点.︿( ̄︶ ̄)︿ 题解: 对于每一个点的最远点,就是以这个点为根到所有叶子节点的最长距离.但是如果确定根的话,除了根节点外,只能找到每个节点(度数-1)个子树的最大值,剩下一个子树是该节点当前的父亲节点. 所以当前节点的最远点在当前节点子树的所有叶子节点以及父亲节点的最远点上(当父亲节点的最远点不在当前节点的子树上时), 如果父亲节