This topic is also a typical minimum spanning tree algorithm, which is similar to the previous one. That is to say, it gives you n two-dimensional plane points,
You can add proper edges so that all vertices are on the same Unicom branch, that is, there are paths between all vertices.
The problem is not large. You can use the prim algorithm to store data in a matrix. The "weight" between any two points is
The distance between two points. 1 # include <stdio. h>
2 # include <stdlib. h>
3 # include <math. h>
4 # include <string. h>
5 const double MAX = 1000000000.0;
6 struct Point
7 {
8 double x, y;
9} point [2, 101];
10
11 double map [0, 101] [101];
12 int v [101], n;
13
14 double Dis (Point a, Point B)
15 {
16 return sqrt (. x-B. x) * (. x-B. x) + (. y-B. y) * (. y-B. y ));
17}
18
19 void Build ()
20 {
21 memset (map, 0, sizeof (map ));
22 for (int I = 0; I <n; I ++)
23 {
24 for (int j = I; j <n; j ++)
25 {
26 if (I = j) map [I] [j] = MAX;
27 else
28 {
29 map [j] [I] = map [I] [j] = Dis (point [I], point [j]);
30}
31}
32}
33}
34
35 void MinTree ()
36 {
37 double sum = 0.0, min;
38 memset (v, 0, sizeof (v ));
39 v [0] = 1;
40 int flag;
41 for (int I = 1; I <n; I ++)
42 {
43 min = MAX;
44 for (int j = 0; j <n; j ++)
45 {
46 if (! V [j] & map [0] [j] <min)
47 {
48 min = map [0] [j];
49 flag = j;
50}
51}
52 sum + = min;
53 v [flag] = 1;
54 for (int j = 0; j <n; j ++)
55 {
56 if (! V [j] & map [0] [j]> map [flag] [j])
57 {
58 map [0] [j] = map [flag] [j];
59}
60}
61}
62 printf ("%. 2lf \ n", sum );
63}
64 int main ()
65 {
66 while (scanf ("% d", & n )! = EOF)
67 {
68 map [n] [n];
69 point [n];
70 for (int I = 0; I <n; I ++)
71 {
72 scanf ("% lf", & point [I]. x, & point [I]. y );
73}
74 Build ();
75 MinTree ();
76}
77 return 0;
78}
79