Game

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 584    Accepted Submission(s): 170

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

/* ***********************************************
Author :CKboss
Created Time :2015年06月07日 星期日 16时39分51秒
File Name :HDOJ5239.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; typedef long long int LL; const int maxn=100100; int n,m; struct Edge
{
int to,next;
}edge[maxn*2]; int Adj[maxn],Size; void init()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
} void Add_Edge(int u,int v)
{
edge[Size].next=Adj[u];
edge[Size].to=v;
Adj[u]=Size++;
} LL val[maxn],sumv[maxn];
priority_queue<LL> q; LL dfs(int u,int fa)
{
LL pos=0;
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(v==fa) continue;
sumv[v]=dfs(v,u);
if(sumv[v]>sumv[pos]) pos=v;
}
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(v==pos||v==fa) continue;
q.push(sumv[v]);
}
sumv[u]=val[u]+sumv[pos];
return sumv[u];
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int T_T,cas=1;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d%d",&n,&m);
init();
for(int i=1;i<=n;i++)
{
scanf("%I64d",val+i);
}
for(int i=0,u,v;i<n-1;i++)
{
scanf("%d%d",&u,&v);
Add_Edge(u,v); Add_Edge(v,u);
}
while(!q.empty()) q.pop();
dfs(1,1);
q.push(sumv[1]);
LL ans=0;
while(!q.empty()&&m--)
{
ans += q.top();
q.pop();
} printf("Case #%d: %I64d\n",cas++,ans);
} return 0;
}

HDOJ 5242 Game的更多相关文章

  1. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  3. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  4. HDOJ 1004 Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  5. hdoj 1385Minimum Transport Cost

    卧槽....最近刷的cf上有最短路,本来想拿这题复习一下.... 题意就是在输出最短路的情况下,经过每个节点会增加税收,另外要字典序输出,注意a到b和b到a的权值不同 然后就是处理字典序的问题,当松弛 ...

  6. HDOJ(2056)&HDOJ(1086)

    Rectangles    HDOJ(2056) http://acm.hdu.edu.cn/showproblem.php?pid=2056 题目描述:给2条线段,分别构成2个矩形,求2个矩形相交面 ...

  7. 继续node爬虫 — 百行代码自制自动AC机器人日解千题攻占HDOJ

    前言 不说话,先猛戳 Ranklist 看我排名. 这是用 node 自动刷题大概半天的 "战绩",本文就来为大家简单讲解下如何用 node 做一个 "自动AC机&quo ...

  8. 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design

    题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...

  9. BFS(八数码) POJ 1077 || HDOJ 1043 Eight

    题目传送门1 2 题意:从无序到有序移动的方案,即最后成1 2 3 4 5 6 7 8 0 分析:八数码经典问题.POJ是一次,HDOJ是多次.因为康托展开还不会,也写不了什么,HDOJ需要从最后的状 ...

随机推荐

  1. 使用开源库 EasyTimeline 操作定时器 NSTimer

    EasyTimeline https://github.com/mmislam101/EasyTimeline Sometimes you need things to happen at speci ...

  2. Android之ListView和GridVIew加载图片

    清除缓存:ImageLoader 对象 . clearCache(); 使用: ImageLoader loader = new ImageLoader(ApplicationContext cont ...

  3. Maximum Likelihood Method最大似然法

    最大似然法,英文名称是Maximum Likelihood Method,在统计中应用很广.这个方法的思想最早由高斯提出来,后来由菲舍加以推广并命名. 最大似然法是要解决这样一个问题:给定一组数据和一 ...

  4. @Logback简介

    Ceki Gülcü在Java日志领域世界知名.他创造了Log4J,这个最早的Java日志框架即便在JRE内置日志功能的竞争下仍然非常流行.随后他又着手实现SLF4J这个"简单的日志前端接口 ...

  5. iOS开发-舒尔特表

    周末闲来无事,看一个概念,挺有意思的,舒尔特表,网上也有很多人写过类似的Demo,本人闲来无事也写了一下,舒尔特表听起来很高大上的样子,不过本人的理解就是一个正方形的矩阵中放的各种小格子,可以是字母, ...

  6. C# 开发者代码审查清单

    这是为C#开发者准备的通用性代码审查清单,可以当做开发过程中的参考.这是为了确保在编码过程中,大部分通用编码指导原则都能注意到.对于新手和缺乏经验(0到3年工作经验)的开发者,参考这份清单编码会很帮助 ...

  7. Android开发之Drag&Drop框架实现拖放手势

    Android3.0提供了drag/drop框架,利用此框架可以实现使用拖放手势将一个view拖放到当前布局中的另外一个view中.本文将介绍如何使用拖放框架. 一.实现拖放的步骤 首先,我们先了解一 ...

  8. HDU OJ Digital Roots 题目1013

     /*Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. 全局 Style

    1.定义一个全局资源文件,如下 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/pres ...

  10. 五条强化 SSH 安全的建议

    当你查看你的 SSH 服务日志,可能你会发现充斥着一些不怀好意的尝试性登录.这里有 5 条常规建议(和一些个别特殊策略)可以让你的 OpenSSH 会话更加安全. 强化密码登录 密码登录很方便,因为你 ...