Jungle Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7005 Accepted Submission(s): 5114
Problem Description
The Head Elder ofthe 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 aboveonthe left shows all the roads in use now andthe cost in aacms per monthto maintain them. Of course there needs to be some way togetbetween all the villages on maintained roads, even ifthe route isnotas short asbefore. The Chief Elder would like totellthe Council of Elders what would be the smallest amount they could spend in aacms per monthto maintain roads that would connect all the villages. The villages are labeled A through I inthe maps above. The map onthe right shows the roads that could be maintained most cheaply, for216 aacms per month. Your task istowrite a program that will solve such problems.
The input consists of one to100 data sets, followed by a final line containing only 0. Each data setstarts with a line containing only a number n, which isthenumberof villages, 1 < n < 27, andthe villages are labeled withthefirst n letters ofthe alphabet, capitalized. Each data setis completed with n-1 lines thatstart with village labels in alphabetical order. There is no line forthelast village. Each line for a village starts withthe village label followed by a number, k, of roads from this village to villages with labels later inthe alphabet. If k isgreater than0, the line continues with data for each ofthe k roads. The data for each road isthe village label forthe other endofthe road followed bythe monthly maintenance cost in aacms forthe road. Maintenance costs will be positive integers less than100. All data fields inthe 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 (beforeorafterinthe alphabet). In the sample input below, thefirst data set goes withthe map above.
The output is one integer per line for each data set: the minimum cost in aacms per monthto maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible setof roads will not finish within the one minute time limit.
Sample Input
9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 353
A 2 B 10 C 40
B 1 C 200
Sample Output
21630
Source
Jungle RoadsTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7005 Accepted Submission(s): 5114Problem DescriptionThe Head Elder of the tropi