树形DP--HihoCoder 1104 Suzhou Adventure

来源:互联网 发布:蚁群算法背包问题 编辑:程序博客网 时间:2024/05/16 10:18

#1104 : Suzhou Adventure

Time Limit:10000ms
Case Time Limit:1000ms
Memory Limit:256MB

Description

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.

Input

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.

Output

The maximum scores Little Hi can get. If there is no solution output -1.

Sample Input
5 2 41 2 3 4 53 41 21 31 42 5
Sample Output
10



        题目的大意是在一棵以1为根的树上拜访m个节点。每个节点都有一个价值。但是其中的k个节点是一定要被拜访的,不管它们的价值是多少。求最大价值。


        题读一半都知道是树形DP了,然而比较蛋疼的是有部分节点必须要被拜访。这个和那道驾车自驾游的问题有点相似,如果用那样的方法的话代码写起来可真是日了狗了。。。。。怎么破啊,懒啊!不想那么敲啊!


        瞬间想到一个“可行”的方法:dpij表示的是以i为子树,拜访j个节点,其中这个子树中所有的被要求必须去的节点都被拜访,所能达到的最大价值。每个子树上有多少个必须被拜访的节点是很容易计算出来的。然而,三秒钟之后这个办法就被抛弃,甚至羞愧于居然能想到这么个“办法”。->好像能转移起来似的!


        “不知道咋整,就滚回去读题。如果你觉得这道题的模型熟悉,那就一定是有一点点细微的差别。”好吧,咱们回去看看。


        The scores are between 1 to 100.


        每个节点的分数还是蛮小的,嗯!


        总共才100个节点啊!也就是说总共也就10000分!


        如果咱们把所有被要求去的节点的分数都加上20000,最终在输出答案的时候把多出来的20000再都减下去不就完了吗。。。。。


        如是思考,不到一分钟的时间这道题就从no law to see变成SB逗逼题了= 皿 =。


        dpij表示以i为根节点的子树中,拜访期中j个节点能达到的最大价值。初始化各个节点的价值的时候注意修改必须去的节点的值就行了。


        上代码:


#include<iostream>#include<string.h>#include<stdlib.h>#include<stdio.h>#include<algorithm>#include<vector>#define inf -2147483647using namespace std;vector<int>e[101];int n,m,k;int dp[101][101];int vv[101];void dfs(int u,int v){dp[u][1] = vv[u];for(int i = 0;i<e[u].size();i++){if(e[u][i] == v)continue;dfs(e[u][i],u);for(int x = m;x >= 1;x--){if(dp[u][x] > inf)for(int y = 0;y <= m - x;y++)if(dp[e[u][i]][y] != inf)dp[u][x+y] = max(dp[u][x+y],dp[u][x] + dp[e[u][i]][y]);}}}int main(){int t;cin>>n>>k>>m;for(int i = 1;i<=n;i++)cin>>vv[i];for(int i = 1;i<=k;i++)cin>>t,vv[t] += 20000;//嘿嘿for(int i = 1;i<n;i++){int t1,t2;cin>>t1>>t2;e[t1].push_back(t2);e[t2].push_back(t1);}if(k > m)//这是充分必要条件,直接干掉得了。但是注意这个部分绝对不可以和上面的for循环换过来。不然人家输入还没完事儿咱们就return 0了。        {cout<<-1<<endl;return 0;}for(int i = 0;i<=100;i++)for(int j = 0;j<=100;j++)dp[i][j] = inf;dfs(1,0);cout<<dp[1][m] - 20000 * k<<endl;return 0;}

        如果你之前没有接触过树形动态规划,那么dfs的部分可能稍微有一点难以理解。我有一篇HiHoCoder 1063的博文,也是一个树形动态规划,那里说了一些关于如何理解,感兴趣的话可以看一下。

0 0
原创粉丝点击