The meaning of this question is to give you a map with n villages. map [I] [j] indicates the distance from village I to Village j, and then give it to you.
M existing roads, so that you can add appropriate roads on this basis, so that all villages are connected, and find the shortest distance for adding roads
Value. Typical use of the Minimum Spanning Tree Algorithm. 1 # include <stdio. h>
2 # include <stdlib. h>
3 # include <string. h>
4
5 const int MAX = 0x7fffffff;
6 int map [101] [101], v [101], x, y, n, sum, flag, min;
7
8 void Reset (int n)
9 {// create a map based on the size of n!
10 for (int I = 0; I <n; I ++)
11 {
12 for (int j = 0; j <n; j ++)
13 {
14 scanf ("% d", & map [I] [j]);
15 if (I = j) map [I] [j] = 0;
16}
17}
18 int m;
19 scanf ("% d", & m );
20 for (int I = 0; I <m; I ++)
21 {
22 scanf ("% d", & x, & y );
23 map [x-1] [Y-1] = map [Y-1] [x-1] = 0;
24}
25}
26 void MinTree ()
27 {// Minimum Spanning Tree Algorithm
28 memset (v, 0, sizeof (v ));
29 v [0] = 1;
30 sum = 0;
31 for (int I = 1; I <n; I ++)
32 {
33 min = MAX;
34 for (int j = 0; j <n; j ++)
35 {
36 if (! V [j] & map [0] [j] <min)
37 {
38 min = map [0] [j], flag = j;
39}
40}
41 v [flag] = 1;
42 sum + = min;
43 for (int j = 0; j <n; j ++)
44 {
45 if (! V [j] & map [0] [j]> map [flag] [j])
46 {
47 map [0] [j] = map [flag] [j];
48}
49}
50}
51 printf ("% d \ n", sum );
52}
53
54 int main ()
55 {
56 while (scanf ("% d", & n )! = EOF)
57 {
58 Reset (n );
59 MinTree ();
60}
61 return 0;
62}