zoukankan      html  css  js  c++  java
  • HDOJ 5242 Game


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


    /* ***********************************************
    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
    ",cas++,ans); 
    	}
    
        return 0;
    }
    


  • 相关阅读:
    初学Python3
    性能测试学习成长图
    k8s集群部署mysql(docker自创建镜像)
    docker 部署uwgsi+python 启动报错 Python 3 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)
    jenkins 打安卓包 cpu使用过高处理操作
    docker部署mysql,nginx,php,并上传镜像到私有仓库
    Linux下PHP7.2扩展
    docker部署Eurake服务,服务节点无法注册服务
    本地Pycharm将spark程序发送到远端spark集群进行处理
    spark集群安装并集成到hadoop集群
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5354288.html
Copyright © 2011-2022 走看看