C language source code: [cpp] # include <stdio. h> # include <stdlib. h> # define maxsize 100 typedef struct edge {int a, B; int weight;} edge; edge finished [maxsize * maxsize/2], notfinished [maxsize * maxsize/2]; int T [maxsize]; 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) {edge * aa = (edge *) a; edge * bb = (edge *) B; return aa-> Weight-bb-> weight;} int main () {int n, a, B, c, d, topf, topn, min, I, roota, rootb; scanf ("% d", & n); while (n) {topf = 0; topn = 0; for (I = 1; I <= n * (n-1) /2; I ++) {scanf ("% d", & a, & B, & c, & d ); if (d = 0) {notfinished [topn]. a = A-1; notfinished [topn]. B = B-1; notfinished [topn ++]. weight = c;} else {finished [topf]. a = A-1; finished [topf]. B = B-1; finished [topf ++]. weight = c ;}for (I = 0; I <n; I ++) T [I] =-1; for (I = 0; I <topf; I + +) {Roota = findroot (finished [I]. a); rootb = findroot (finished [I]. B); if (roota! = Rootb) T [rootb] = roota;} min = 0; www.2cto.com qsort (notfinished, topn, sizeof (notfinished [0]), cmp); for (I = 0; I <topn; I ++) {roota = findroot (notfinished [I]. a); rootb = findroot (notfinished [I]. b); if (roota! = Rootb) {T [rootb] = roota; min + = notfinished [I]. weight ;}} printf ("% d \ n", min); scanf ("% d", & n );}}