星河战队(Starship Troopers)记录

来源:互联网 发布:p2p理财网站排名知乎 编辑:程序博客网 时间:2024/04/28 17:26

Starship Troopers

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6816    Accepted Submission(s): 1845


Problem Description
You, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built underground. It is actually a huge cavern, which consists of many rooms connected with tunnels. Each room is occupied by some bugs, and their brains hide in some of the rooms. Scientists have just developed a new weapon and want to experiment it on some brains. Your task is to destroy the whole base, and capture as many brains as possible.

To kill all the bugs is always easier than to capture their brains. A map is drawn for you, with all the rooms marked by the amount of bugs inside, and the possibility of containing a brain. The cavern's structure is like a tree in such a way that there is one unique path leading to each room from the entrance. To finish the battle as soon as possible, you do not want to wait for the troopers to clear a room before advancing to the next one, instead you have to leave some troopers at each room passed to fight all the bugs inside. The troopers never re-enter a room where they have visited before.

A starship trooper can fight against 20 bugs. Since you do not have enough troopers, you can only take some of the rooms and let the nerve gas do the rest of the job. At the mean time, you should maximize the possibility of capturing a brain. To simplify the problem, just maximize the sum of all the possibilities of containing brains for the taken rooms. Making such a plan is a difficult job. You need the help of a computer.
 

Input
The input contains several test cases. The first line of each test case contains two integers N (0 < N <= 100) and M (0 <= M <= 100), which are the number of rooms in the cavern and the number of starship troopers you have, respectively. The following N lines give the description of the rooms. Each line contains two non-negative integers -- the amount of bugs inside and the possibility of containing a brain, respectively. The next N - 1 lines give the description of tunnels. Each tunnel is described by two integers, which are the indices of the two rooms it connects. Rooms are numbered from 1 and room 1 is the entrance to the cavern.

The last test case is followed by two -1's.
 

Output
For each test case, print on a single line the maximum sum of all the possibilities of containing brains for the taken rooms.
 

Sample Input
5 1050 1040 1040 2065 3070 301 21 32 42 51 120 7-1 -1
 

Sample Output
507

描述

你,作为星河战队的队长,被派去消灭一个臭虫的基地。这个基地被建立在地下。这是一个相当大的洞穴,由许多房间以及房间之间的通道组成。每个房间被一些臭虫占据,其中,大脑虫们(比较牛X的臭虫)藏在一些特定的房间中。科学家已经开发了一种新的武器,想要在大脑虫身上进行试验。你的任务是去摧毁整个基地,并且俘获尽可能多的大脑虫。

消灭所有的臭虫,总比俘获大脑虫来得容易。你手上有一张地图,地图上标出了每个房间里臭虫的数量,以及大脑虫可能藏身的地方。整个洞穴是树形的结构,每个房间到出口只有唯一的一条通路。你想要快速解决战斗,所以等不及消灭干净一个房间,再向下一个房间前进,取而代之的是在通过某房间时你可以留下一部分部队来消灭房间里的臭虫。部队不会重复进入一个已经被访问过的房间。

一个星河小队可以消灭20个臭虫。由于没有足够的部队数量,你只能选择一些房间来派遣部队,剩下的房间只能由毒气来清理。同时,你应该尽可能多地俘获大脑虫。为了简化问题,只需使得的被部队消灭的房间里,所含的可能大的脑虫数量和最大。制定这样的计划不是简单的任务,你需要计算机的帮助。

输入

输入包括多组测试数据。第一行包括2个整数N(0 < N <= 100) 和M(0 <= M <= 100), 分别表示基地中房间的数量和你所拥有的星河小队的数量。接下来N行给出了每个基地房间的描述。每行包含两个非负整数,分别表示房间里臭虫的数量和可能含有的大脑虫的数量。再接下来N-1行给出了房间通道的描述,包括两个整数,分别表示通道两端连接的房间的标号。房间标号从1开始,并且房间1就是整个洞穴的入口。
最后一组测试数据由2个-1组成,表示结束。

输出

每组测试数据输出一行,包括一个整数,表示最多可能俘获的大脑虫数目

提示


题目来源

HDU http://acm.hdu.edu.cn/showproblem.php?pid=1011


import java.util.ArrayList;import java.util.List;import java.util.Scanner;/** * for 杭电 ACM1011 * @author  718225250 * */public class Main{public static class Cavern{Integer id;int bugs;int brains;int troopers;boolean isPass = false;//是否已经过List<Integer> next = new ArrayList<Integer>();public Cavern(int id, int bugs, int brains){this.id = Integer.valueOf(id);this.bugs = bugs;this.brains = brains; this.troopers = (bugs%20==0 ? bugs/20 : bugs/20+1);}}/** * 获得一次遍历的最大值 * @return */public static int getMax(){int Max =curMax;int id = _list.get(_list.size()-1);boolean hasNextForPass = false;/*是否还有下一个节点供遍历*/for(int i=0;i< _caverns[id].next.size();i++){Integer nextId = _caverns[id].next.get(i);if(!_caverns[nextId].isPass){hasNextForPass = true;_list.add(nextId);_caverns[nextId].isPass = true;Max = getMax();_caverns[nextId].isPass = false;_list.remove(_list.size()-1);}}if(!hasNextForPass){int tempMax = getResult() ;if(tempMax > Max){curMax = tempMax;Max = curMax;}}return Max;}/** * 获得结果 * @return */public static int getResult(){int result = 0;int tempTrooperNumber = __TrooperNumber;int tempResult = 0;boolean flag = true;for(int i =0; i < _list.size(); i++ ){Integer id = _list.get(i);if(tempTrooperNumber >= _caverns[id].troopers){tempTrooperNumber -= _caverns[id].troopers;tempResult += _caverns[id].brains;}else{flag = false;break;}}if(flag){result = tempResult;}else{tempResult = 0;for(int i =0; i < _list.size(); i++ ){Integer id = _list.get(i);_list.remove(id);tempResult = getResult();if(tempResult > result){result = tempResult;}_list.add(id);}}return result;}public static int __CavernNumber;public static int __TrooperNumber; public static Cavern[] _caverns;public static int curMax;public static final  List<Integer> _list = new ArrayList<Integer>();public static void main(String[] args){Scanner in=new Scanner(System.in);while(true){__CavernNumber = in.nextInt();__TrooperNumber = in.nextInt();if( __CavernNumber < 0 && __TrooperNumber < 0 ){break;} else if(__CavernNumber == 0 || __TrooperNumber == 0){System.out.println(0);}else{_caverns = new Cavern[__CavernNumber];for(int i=0;i<__CavernNumber;i++){int bugs = in.nextInt();int brains = in.nextInt();_caverns[i] = new Cavern(i, bugs, brains);}for(int i=0;i<__CavernNumber-1;i++){int m = in.nextInt();int n = in.nextInt();_caverns[m-1].next.add(n-1);_caverns[n-1].next.add(m-1);}curMax = 0;Integer start = Integer.valueOf(0);_caverns[0].isPass = true;_list.add(start);System.out.println(getMax());}}}}