C language source code: [cpp] # include <stdio. h> # include <stdlib. h> # define maxsize 1000 typedef struct edge {int a, B; int weight;} edge; edge E [maxsize]; int T [27]; int findroot (int x) {int temp; if (T [x] =-1) return x; else {temp = findroot (T [x]); T [x] = temp; return temp ;}} int cmp (const void * a, const void * B) {struct edge * aa = (edge *) a; struct edge * bb = (edge *) b; return aa-> weight-bb-> weight;} int main () {int N, weight, k, I, j, top, roota, rootb, min; char point1, point2; scanf ("% d", & n); getchar (); while (n) {top = 0; for (I = 0; I <n-1; I ++) {point1 = getchar (); scanf ("% d ", & k); getchar (); while (k --) {point2 = getchar (); scanf ("% d", & weight); getchar (); E [top]. a = point1-'A'; E [top]. B = point2-'A'; E [top ++]. weight = weight ;}} qsort (E, top, sizeof (E [0]), cmp); for (I = 0; I <n; I ++) T [I] =-1; min = 0; for (I = 0; I <top; I ++) {www. 2cto.com roota = findroot (E [I]. a); rootb = findroot (E [I]. B); if (roota! = Rootb) {T [rootb] = roota; min + = E [I]. weight ;}} printf ("% d \ n", min); scanf ("% d", & n); getchar ();}}