Package nyoj; import Java. util. extends; public class main {public static void main (string ARGs []) {// system. out. println (integer. max_value); Comment SCN = new comment (system. in); int Len = SCN. nextint (); While (Len --> 0) {int v = SCN. nextint (); int e = SCN. nextint (); Boolean visit [] = new Boolean [V + 1]; // whether the vertex is accessed. Int map [] [] = new int [V + 1] [V + 1]; // The distance between the two points int low [] = new int [V + 1]; // read the map // initialize the array to a heartless big for (INT I = 1; I <v + 1; I ++) {for (Int J = 1; j <v + 1; j ++) {map [I] [J] = integer. max_value; if (I = J) map [I] [I] = 0 ;}}for (INT I = 0; I <E; I ++) {int x = SCN. nextint (); int y = SCN. nextint (); map [x] [Y] = map [y] [x] = SCN. nextint ();} int ans = prime (MAP, V, E); system. out. println (ANS); int start = SCN. nextint (); For (INT I = 1; I <v; I ++) {int TEM = SCN. nextint (); If (TEM <start) Start = TEM;} // system. out. println (ANS + start) ;}} Private Static int prime (in T [] [] map, int V, int e) {// todo auto-generated method stub Boolean visit [] = new Boolean [V + 1]; // whether the vertex is accessed int low [] = new int [V + 1]; visit [1] = true; int ans = 0; // initialize low, is the distance from the set to the for (INT I = 1; I <= V; I ++) {If (! Visit [I]) {LOW [I] = map [1] [I] ;}// add one at a time, v-1 times for (INT I = 2; I <= V; I ++) {// select the lowest point int K = 0 in the non-Access Node; int min = 1 <31-1; for (Int J = 1; j <= V; j ++) {If (! Visit [J] & low [J] <min) min = low [k = J];} ans + = min; // select the smallest edge // select K nodes visit [k] = true; // update the vertex starting with K for (I = 1; I <v + 1; I ++) {If (! Visit [I] & map [k] [I] <low [I]) {LOW [I] = map [k] [I] ;}} return ans ;}}
[To be modified] Minimum Spanning Tree of nyoj 38