Hdu 5242 Game【Dfs+贪心】好题~

来源:互联网 发布:淘宝seo冷门词 编辑:程序博客网 时间:2024/05/18 10:00

Game

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


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 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.
 

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 the t-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条链,使得链上包含的所有点的权值和最大。


思路:


①我们先反向建图,跑出各个点到根节点1的距离。


②然后我们按照各个节点到根节点1的距离从大到小排序,然后我们贪心的从大的点开始向根走,走过的路我们标记上,使得之后再走这条路的话获得的权值为0.

维护一个数组ans【i】,表示这个过程中,从节点i跑到根节点能够获得的价值。


③那么我们得到了一个数组ans【i】之后,我们再将其进行排序,然后贪心的取最大的K个值即可。


Ac代码:

#include<stdio.h>#include<string.h>#include<vector>#include<queue>#include<algorithm>using namespace std;#define ll __int64struct node{    ll len;    int u;}Node[150000];vector<int>mp[150000];ll ans[150000];ll sum[150000];ll val[150000];int degree[150000];int n,p;int cmp(node a,node b){    return a.len>b.len;}ll Dfs(int u){    if(sum[u]>0)return sum[u];    sum[u]=val[u];    for(int i=0;i<mp[u].size();i++)    {        int v=mp[u][i];        sum[u]+=Dfs(v);    }    return sum[u];}ll Dfs2(int u){    if(sum[u]>0)return 0;    sum[u]=val[u];    for(int i=0;i<mp[u].size();i++)    {        int v=mp[u][i];        sum[u]+=Dfs2(v);    }    return sum[u];}int main(){    int t;    int kase=0;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&p);        memset(ans,0,sizeof(ans));        memset(degree,0,sizeof(degree));        memset(sum,0,sizeof(sum));        for(int i=1;i<=n;i++)mp[i].clear();        for(int i=1;i<=n;i++)scanf("%I64d",&val[i]);        for(int i=1;i<=n-1;i++)        {            int x,y;            scanf("%d%d",&x,&y);            mp[y].push_back(x);            degree[x]++;        }        for(int i=1;i<=n;i++)        {            Node[i].u=i;            Node[i].len=Dfs(i);        }        sort(Node+1,Node+1+n,cmp);        memset(sum,0,sizeof(sum));        for(int i=1;i<=n;i++)        {            ans[i]=Dfs2(Node[i].u);        }        ll output=0;        sort(ans+1,ans+1+n);        for(int i=n;i>n-p;i--)        {            output+=ans[i];        }        printf("Case #%d: %I64d\n",++kase,output);    }}











原创粉丝点击