hdu5242Game(好题)

Game

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



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

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

Source
The 2015 ACM-ICPC China Shanghai Metropolitan Programming Contest
题目利用了树链剖分的思想加上贪心
#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+100;
vector<int>G[maxn];
__int64 a[maxn],dis[maxn],dis1[maxn];
int cnt,cnt1,fa[maxn],vis[maxn];
struct node{
    int num;
    __int64 dis;
}line[maxn];

void dfs1(int u){
    int flag=0;
    dis[u]=dis[fa[u]]+a[u];
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(v==fa[u]){
            continue;
        }
        flag=1,fa[v]=u;
        dfs1(v);
    }
    if(flag==0){
        line[cnt].num=u;
        line[cnt++].dis=dis[u];
    }
}

bool cmp(node x,node y){
    return x.dis>y.dis;
}

bool cmp1(__int64 x,__int64 y){
    return x>y;
}

void dfs2(int u){
    if(vis[u]==1||u==0)
        return ;
    vis[u]=1;
    dis1[cnt1]+=a[u];
    dfs2(fa[u]);
}

int main(){
    int n,k,_;
    //freopen("5242.in","r",stdin);
    scanf("%d",&_);
    for(int case1=1;case1<=_;case1++){
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++){
            scanf("%I64d",&a[i]);
            G[i].clear();
        }
        int u,v;
        for(int i=1;i<n;i++){
            scanf("%d%d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        fa[1]=0,dis[0]=0,cnt=0;
        dfs1(1);
        sort(line,line+cnt,cmp);
        memset(vis,0,sizeof(vis));
        memset(dis1,0,sizeof(dis1));
        cnt1=0;
        for(int i=0;i<cnt;i++){
            dfs2(line[i].num);
            cnt1++;
        }
        sort(dis1,dis1+cnt1,cmp1);
        __int64 ans=0;
        for(int i=0;i<k&&i<cnt1;i++){
            ans+=dis1[i];
        }
        printf("Case #%d: %I64d\n",case1,ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值