Attention:

Note Array out-of-bounds problem (commit appears runtimeerror represents array out of bounds)

At the beginning of the submission, the number of edges in the edge and the number of points in the point set are defined by the same macro, but the macro definition is defined by the maximum number of points, so there is an array out-of-bounds problem at the time of submission, so you need to pay attention later.

Description

The Head Elder of the tropical island of Lagrishan have a problem. A burst of foreign aid money is spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly and so the large road network are 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 with use now and the cost in aacms per month to maintain them. Of course there needs to be some-to-get between all the villages on maintained roads, even if the route was not as Shor T as before. The chief Elder would like to tell the Council of the elders what would is the smallest amount they could spend in aacms per m Onth to maintain roads this would connect all the villages. The villages is labeled A through I in the maps above. The map on the right shows the roads that could is maintained most cheaply, for 216 aacms per month. Your task is to write a program that would solve such problems.

Input

The input consists of one to the 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 <, and the VI Llages is labeled with the first n letters of the alphabet, capitalized. Each data set was completed with n-1 lines this start with the 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 was the village label for the other end of the road followed by the monthly maintenance cost in AACM s for the road. Maintenance costs is positive integers less than 100. All data fields in the row is separated by single blanks. The road network would always be allow to travel between all the villages. The network would never have more than roads. No Village'll has more than roads going to other villages (before or after the alphabet). In the sample input below, the first data set goes with the map above.

Output

The output is an integer per line for each data set:the minimum cost of aacms per month to maintain a road system that C Onnect all the villages. Caution:a Brute force solution that examines every possible set of roads would not finish within the one minute time limit .

Sample Input

9A 2 B I 25B 3 c ten H i 8C 2 D 55D 1 E 44E 2 F g 38F 0G 1 H 35H 1 I 353A 2 B ten C 40B 1 C 200

Sample Output

21630

//RuntimeError errors occur when the array is out of bounds, note that the number of edges is different from the number of points, and when a macro is defined, notice that the array is out of bounds ./*Test Instructions: Multiple groups of cases the first line of each group of cases enter a number n the first data in each row of the N-1 line is a character start, and the character from A to the second of each row is a number num, Indicates that a node with NUM nodes is connected to the first character of the line. The next data for each row is the NUM group End,cost, which indicates that the start to end cost for the specific input and output look at the case will understand the solution: Kruskal algorithm*/#include<stdio.h>#include<algorithm>#include<stdlib.h>#include<iostream>using namespacestd;Const intmaxn= -;/*Edge Structure*/typedefstruct{ intStart//Road Start intEnd//End of Road DoubleValue//Road Weight Value}edge; Edge road[ the];/*Node Collection*/intNODE[MAXN];/*Root -seeking function*/intFind_set (intN) { if(node[n]==-1)returnN; returnNode[n] = = n?Node[n]: Find_set (Node[n]); //return Node[n]=find_set (Node[n]);}/*comparison functions in sorting*/BOOLCMP (Edge A,edge b) {if(A.value<b.value)return true; return false;}/*merging: Merging trees into a tree*/BOOLMerge (intAintb) { intr1=Find_set (a); intR2=Find_set (b); if(R1==R2)return false; if(R1<R2) node[r2]=R1; if(R2<R1) node[r1]=R2; return true;}/*Kruskal Algorithm*/intKruskal (intNintM) {//N vertex number M Edge number intnum=0; intcost=0; Sort (Road,road+m,cmp); for(intI=0; i<m;i++){ if(Merge (road[i].start,road[i].end)) {num++; Cost+=Road[i].value; } if(num==n-1) Break; } if(num!=n-1)return-1;//cannot produce minimum spanning tree Else returnCost ;}intMain () {intN; //freopen ("Input.txt", "R", stdin); while(SCANF ("%d", &n)! =EOF) { if(n = =0) Break; for(inti =0; I < n; i++) Node[i]=i; CharS, E; intnum, cost, k =0; for(inti =0; I < n1; i++) {cin>> s >>num; for(intj =0; J < num; J + +, k++) {cin>> e >>Cost ; Road[k].start= S-'A'; Road[k].end= e-'A'; Road[k].value=Cost ; }} sort (road, road+K, CMP); intRET =Kruskal (n, k); printf ("%d\n", ret); } return 0;}

Kruskal algorithm to find the minimum spanning tree (Kruskal solution of jungle Roads)