Jungle Roads
Time Limit: 1000MS | Memory Limit: 10000K | |
---|---|---|
Total Submissions: 33100 | Accepted: 15473 |
Description
The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.Input
The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above.
Output
The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.
Sample Input
9A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E 44E 2 F 60 G 38F 0G 1 H 35H 1 I 353A 2 B 10 C 40B 1 C 200
Sample Output
21630
丛林中的道路
题目描述
热带岛屿Lagrishan的长老们遇到了一个问题。在几年前,有一大笔外援资金被用在了连接各个村庄之间道路上,可是有些道路是多余的,这就意味着岛上要用一大笔资金来维护这些累赘的道路。更糟的是,这些道路还穿越了从林,长老会显然承担不起这么大的财政开支,所以长老们开会决定,停止维护一些道路。左边的地图展示了现在所有道路以及每个月维护他们所需的费用。当然了,虽然道路没有像以前那么短,但是村庄与村庄之间肯定是连通的。长老会想知道连通所有村庄所需的最少费用。在上图中,每个村庄被从A到I被编号,右图展示了用在道路维护上的最少开支,只要216的花费。你的任务就是编写一个程序来解决这个问题。
Input
输入有多组,至多有100组输入,输入的最后一行只包含一个数字0。每组数据首先输入一个整数n(1 < n < 27),表示有n个村庄,用字母表的前n个大写字母来为每个村庄标号。接下来的n-1行输入,首先输入村庄的编号,表示路的一端,然后输入一个整数k,如果k大于0,那么紧接着的是k条路的数据。每组路的数据首先是道路另一端村庄的编号,然后是路的维护成本,维护成本是一个小于100的正整数。输入保证每个村庄都能到达其他村庄,道路数不超过75条,且没有一个村庄有超过15条与其他村庄连接的道路。在输入样例中,第一组数据即为上图所示的地图。
Output
每组输入对应一行输出,每行输出一个表示最小花费的整数。注意:若检查每种可能的修建方案,程序可能无法在时间限制内结束运行。
Sample Input
9A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E 44E 2 F 60 G 38F 0G 1 H 35H 1 I 353A 2 B 10 C 40B 1 C 200
Sample Output
21630