The question is to give you a graph with n vertices and give n * (n-1)/2 edge information, including the edge endpoint and edge length.
Select the shortest path to build all nodes on the same connected branch. Typical Minimum Spanning Tree Algorithms
If the scale is not large, the direct matrix can be competent. 1 # include <stdlib. h>
2 # include <stdio. h>
3 # include <string. h>
4
5 const int MAX = 100000000;
6 int map [101] [101], MIN, sum;
7 // map [I] [j] records the distance from point I to Point j!
8
9 int main ()
10 {
11 int n, x, y, m, v [101], flag, dis;
12 while (scanf ("% d", & n), n)
13 {
14 map [n] [n];
15 memset (map, 0, sizeof (map); // clear the Array
16 m = (n * (n-1)/2;
17 for (int I = 0; I <m; I ++)
18 {// enter and process edge information
19 scanf ("% d", & x, & y, & dis );
20 map [x-1] [Y-1] = map [Y-1] [x-1] = dis;
21}
22 for (int I = 0; I <n; I ++) map [I] [I] = MAX;
23 // graph creation is complete!
24 v [n];
25 memset (v, 0, sizeof (v ));
26 v [0] = 1;
27 sum = 0;
28 for (int I = 1; I <n; I ++)
29 {// obtain the minimum spanning tree using the prim algorithm
30 MIN = 10000000;
31 for (int j = 0; j <n; j ++)
32 {
33 if (! V [j] & map [0] [j] <MIN)
34 {
35 MIN = map [0] [j];
36 flag = j;
37}
38}
39 sum + = MIN;
40 v [flag] = 1;
41 for (int j = 0; j <n; j ++)
42 {
43 if (! V [j] & map [0] [j]> map [flag] [j])
44 {
45 map [0] [j] = map [flag] [j];
46}
47}
48}
49 printf ("% d \ n", sum );
50}
51 return 0;
52}
53