HDU 5242 Game (贪心/思维)

本文介绍了一款名为“XX岛”的galgame游戏,玩家Katsuragi以其卓越的技能和经验,在游戏中“捕获”虚拟女孩。文章详细解释了游戏的结构,包括场景之间的转换和价值获取机制,并提出了一种算法来计算Katsuragi在有限次数内所能获得的最大总价值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

贪心,但是一开始没有想到,大体就是找出所有路线,排序,从最大的开始走,再不重复的走一次路线,遇到重复的return。再排序,找m项,输出。

必须用long long!!!一开始看到在int范围以为总数在int内。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <string>
#include <vector>
//#include <iostream>
using namespace std;
//for(i=1;i<n;i++)
//scanf("%d",&n);
//printf("\n",);

vector<int>mapp[100005];
vector<int>mmap[100005];
long long dp[100005];
long long num[100005];

struct inf
{
    int pos;long long num;
}infor[100005];


int cmp0(long long a,long long b)
{
    return a>b;
}


int cmp(struct inf q,struct inf w)
{
    return q.num>w.num;
}

bool flg[100005];

int dfs(int st,long long sum)
{
    dp[st]=sum;

    for(int i=0;i<mapp[st].size();i++)
    {
        dfs(mapp[st][i],sum+num[mapp[st][i]]);
    }

    return 0;
}


long long dfss(int st)
{
    flg[st]=1;
    for(int i=0;i<mmap[st].size();i++)
    {
        if(flg[mmap[st][i]]==1)
            return num[st];
        return dfss(mmap[st][i])+num[st];
    }

    return num[st];

}


int main()
{
    int i,j,k,m,n,t,ll;

    scanf("%d",&t);

    for(ll=1;ll<=t;ll++)
    {
        scanf("%d%d",&n,&m);


        for(i=0;i<=n;i++)
        {
            mapp[i].clear();
            mmap[i].clear();
            num[i]=0;
            dp[i]=0;
            flg[i]=0;
            infor[i].num=0;
            infor[i].pos=0;
        }

        for(i=1;i<=n;i++)
        {
            scanf("%lld",&num[i]);
        }

        for(i=1;i<n;i++)
        {
            scanf("%d%d",&j,&k);
            mapp[j].push_back(k);
            mmap[k].push_back(j);
        }


        dfs(1,0);


        for(i=1;i<=n;i++)
        {
            if(mapp[i].size()==0)
            {
                infor[i].num=dp[i];
                infor[i].pos=i;
            }
        }

        sort(infor+1,infor+1+n,cmp);

        for(i=1;i<=n;i++)
        {
            if(infor[i].pos!=0)
            dp[i]=dfss(infor[i].pos);
            else
            dp[i]=0;
        }

        sort(dp+1,dp+1+n,cmp0);

        long long ans=0;

        for(i=1;i<=m;i++)
        {
            ans+=dp[i];
        }

        printf("Case #%d: %lld\n",ll,ans);


    }


    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值