HDU 5242 Game
2015-09-05 15:57
246 查看
[align=left]Problem Description[/align]
It is well known that Keima Katsuragi is The Capturing God because of his exceptional skills and experience in ''capturing'' virtual girls in gal games. He is able to play
k
games simultaneously.
One day he gets a new gal game named ''XX island''. There are
n
scenes in that game, and one scene will be transformed to different scenes by choosing different options while playing the game. All the scenes form a structure like a rooted tree such that the root is exactly the opening scene while leaves are all the ending
scenes. Each scene has a value , and we use wi
as the value of the i-th
scene. Once Katsuragi entering some new scene, he will get the value of that scene. However, even if Katsuragi enters some scenes for more than once, he will get
wi
for only once.
For his outstanding ability in playing gal games, Katsuragi is able to play the game
k
times simultaneously. Now you are asked to calculate the maximum total value he will get by playing that game for
k
times.
[align=left]Input[/align]
The first line contains an integer
T(T≤20),
denoting the number of test cases.
For each test case, the first line contains two numbers
n,k(1≤k≤n≤100000),
denoting the total number of scenes and the maximum times for Katsuragi to play the game ''XX island''.
The second line contains n
non-negative numbers, separated by space. The i-th
number denotes the value of the i-th
scene. It is guaranteed that all the values are less than or equal to
231−1.
In the following n−1
lines, each line contains two integers a,b(1≤a,b≤n),
implying we can transform from the a-th
scene to the b-th
scene.
We assume the first scene(i.e., the scene with index one) to be the opening scene(i.e., the root of the tree).
[align=left]Output[/align]
For each test case, output ''Case #t:'' to represent the
t-th
case, and then output the maximum total value Katsuragi will get.
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
题意:给定一个n个节点的树,每个节点都有权值。每次从根节点出发到叶子节点,可获取所经结点一次的价值,但每个节点价值只能取一次。问走k次能获得的总最大价值
思路:深搜一次,找出最大价值的路径。如果k=1,那么结果一定是这条路径。否则,其余k-1条到叶子节点的路径价值要减去已经走完的节点权值。可以直接将每次遍历的结果存入优先队列中,最后取出前k-1个。详情见代码
It is well known that Keima Katsuragi is The Capturing God because of his exceptional skills and experience in ''capturing'' virtual girls in gal games. He is able to play
k
games simultaneously.
One day he gets a new gal game named ''XX island''. There are
n
scenes in that game, and one scene will be transformed to different scenes by choosing different options while playing the game. All the scenes form a structure like a rooted tree such that the root is exactly the opening scene while leaves are all the ending
scenes. Each scene has a value , and we use wi
as the value of the i-th
scene. Once Katsuragi entering some new scene, he will get the value of that scene. However, even if Katsuragi enters some scenes for more than once, he will get
wi
for only once.
For his outstanding ability in playing gal games, Katsuragi is able to play the game
k
times simultaneously. Now you are asked to calculate the maximum total value he will get by playing that game for
k
times.
[align=left]Input[/align]
The first line contains an integer
T(T≤20),
denoting the number of test cases.
For each test case, the first line contains two numbers
n,k(1≤k≤n≤100000),
denoting the total number of scenes and the maximum times for Katsuragi to play the game ''XX island''.
The second line contains n
non-negative numbers, separated by space. The i-th
number denotes the value of the i-th
scene. It is guaranteed that all the values are less than or equal to
231−1.
In the following n−1
lines, each line contains two integers a,b(1≤a,b≤n),
implying we can transform from the a-th
scene to the b-th
scene.
We assume the first scene(i.e., the scene with index one) to be the opening scene(i.e., the root of the tree).
[align=left]Output[/align]
For each test case, output ''Case #t:'' to represent the
t-th
case, and then output the maximum total value Katsuragi will get.
[align=left]Sample Input[/align]
2 5 2 4 3 2 1 1 1 2 1 5 2 3 2 4 5 3 4 3 2 1 1 1 2 1 5 2 3 2 4
[align=left]Sample Output[/align]
Case #1: 10 Case #2: 11
题意:给定一个n个节点的树,每个节点都有权值。每次从根节点出发到叶子节点,可获取所经结点一次的价值,但每个节点价值只能取一次。问走k次能获得的总最大价值
思路:深搜一次,找出最大价值的路径。如果k=1,那么结果一定是这条路径。否则,其余k-1条到叶子节点的路径价值要减去已经走完的节点权值。可以直接将每次遍历的结果存入优先队列中,最后取出前k-1个。详情见代码
#include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<iostream> #include<map> #include<vector> #include<queue> #include<stack> #include<cstdlib> #include<deque> const int N=100010; const int INF=1500000; typedef long long ll; using namespace std; int n,k,a ;// 数组a为每个节点的权值 ll dp ;// 叶子节点到当前节点的最大价值 vector<int> ve ; priority_queue<ll> q; void dfs(int u) { dp[u]=a[u]; int v,temp; for(int i=0;i<ve[u].size();i++) { v=ve[u][i]; dfs(v); if(dp[u]>dp[v]+a[u]) //如果当前结果不是最优的,存入优先队列中(可能是前k大的路径) { q.push(dp[v]); } else { q.push(dp[u]-a[u]); //否则,将原先的结果存入队列(要减去当前节点的权值),并更新最优结果, dp[u]=dp[v]+a[u]; } } } int main() { int t,cns=0; scanf("%d",&t); while(t--) { memset(dp,0,sizeof(dp)); memset(a,0,sizeof(a)); while(!q.empty()) q.pop(); scanf("%d%d",&n,&k); int i,j,u,v; for(i=1;i<=n;i++) { scanf("%d",&a[i]); ve[i].clear(); } for(i=1;i<n;i++) { scanf("%d%d",&u,&v); ve[u].push_back(v); } dfs(1); ll s=dp[1]; //最大价值初始化(因为最优的结果没有存入优先队列中) for(i=1;i<k;i++) { s+=q.top(); q.pop(); } printf("Case #%d: %lld\n",++cns,s); } return 0; }
相关文章推荐
- ssoj2000平衡点(balance)(退火算法)
- web移动端知识和资源链接
- 矩阵乘法求斐波那契数列(快速幂)
- sharepoint 2016 学习系列篇(5)-创建一个应用程序网站
- replace与replaceAll的区别
- PHP开发Linux桌面应用和Android应用思路
- Regionals 2014 >> Asia - Tokyo
- uvaoj 1362 - Exploring Pyramids
- Django的TEMPLATES设置项(1.8新特性)
- C++内存详解
- UVA10054欧拉回路
- Hibernate-继承映射
- esd win10 /win8 最简单快速的安装方法 双系统、vhd、wimboot+VHD
- 证券交易术语
- 详细研究CSS列表
- 剑指offer:二维数组中的查找 代码实现
- Yii CDBCriteria常用方法
- LeetCode database 之 Second Highest Salary(第二高薪水)
- 数据库重要概念解释
- Effective C++——条款22(第4章)