[To be modified] Minimum Spanning Tree of nyoj 38

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.