hdu 5242 Game

来源:互联网 发布:淘宝网啄木鸟蚕丝被 编辑:程序博客网 时间:2024/06/11 15:45

Game

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1468    Accepted Submission(s): 478


Problem Description
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 playk 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 getwi for only once.

For his outstanding ability in playing gal games, Katsuragi is able to play the gamek times simultaneously. Now you are asked to calculate the maximum total value he will get by playing that game fork times.
 

Input
The first line contains an integer T(T20), denoting the number of test cases.

For each test case, the first line contains two numbers n,k(1kn100000), 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 2311.

In the following n1 lines, each line contains two integers a,b(1a,bn), 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).

 

Output
For each test case, output ''Case #t:'' to represent thet-th case, and then output the maximum total value Katsuragi will get.
 

Sample Input
25 24 3 2 1 11 21 52 32 45 34 3 2 1 11 21 52 32 4
 

Sample Output
Case #1: 10Case #2: 11
 


题意

给出一颗以1节点为根的树,每个节点有各自的价值,有k次从根节点出发向下走到叶子节点的机会,每次会得到所有经过节点的权值,每个节点只有在第一次经过时有价值,求k次之后能够获得的最大权值。


解题思路

利用记忆化搜索求出每个点到1号节点产生的价值,(相当于把所有路都分出来了,)将其按照价值降序排序。然后在根据这个降序序列按照每个点只能加分一次计算n个值,将这n个值在进行降序排序,取前k个即可。


代码

#include <iostream>#include <cstdio>#include <vector>#include <cstring>#include <algorithm>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 100000+10;struct node{    int num;///点的编号    long long sum_val;///这个点到1号点的最大值}max1[maxn];int fa[maxn];///存图long long val[maxn];long long ans[maxn];bool used[maxn];void init(){    for(int i = 0;i<maxn;++i){        fa[i] = i;        max1[i].num = i;        max1[i].sum_val = 0;        ans[i] = 0;        used[i] = false;    }}int n,k;long long dfs1(int x){    if(max1[x].sum_val) return max1[x].sum_val;    return max1[x].sum_val =  dfs1(fa[x]) + val[x];}bool cmp1(node a,node b){    return a.sum_val > b.sum_val;}bool cmp2(long long a,long long b){    return a>b;}long long dfs2(int x){    if(used[x]) return 0;    used[x] = true;    return dfs2(fa[x]) + val[x];}int main(){    int t,a,b;    scanf("%d",&t);    for(int icase = 1; icase <= t;++icase){        init();        scanf("%d%d",&n,&k);        for(int i = 1;i<=n;++i){            scanf("%d",&val[i]);        }        max1[1].sum_val = val[1];        for(int i = 1; i<n;++i){            scanf("%d%d",&a,&b);            fa[b] = a;        }        for(int i = 2; i<=n;++i){            max1[i].sum_val = dfs1(i);        }        sort(max1,max1+n+1,cmp1);        for(int i = 0;i<n;++i){            ans[i] = dfs2(max1[i].num);        }        sort(ans,ans+n,cmp2);        long long  sum = 0;        for(int i = 0;i<k;++i){            sum+=ans[i];        }        printf("Case #%d: ",icase);        printf("%lld\n",sum);    }    return 0;}



1 0