Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the minimum spanning Tree with java kruskal

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to achieve the minimum spanning tree in java kruskal". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve the minimum spanning tree in java kruskal".

Say no more, look at the code:

Import java.util.ArrayList;import java.util.Collections;import java.util.Scanner;/**P1861 Network D question-minimum spanning tree: kruskal4 61 2 11 3 114 22 3 13 4 12 4 1141 21 32 33 4 * @ author Yao Lintao * * / public class Main {static int NMagi M; static int [] SET; / / and look up array static ArrayList lines; / / graph storage, edge set storage static boolean [] visited / / access tag static int maxLine; / / maximum distance public static void main (String [] args) {Scanner sc = new Scanner (System.in); / / receive parameter N = sc.nextInt (); M = sc.nextInt (); maxLine = 0 SET = new int [Numb1]; visited = new boolean [M]; lines = new ArrayList (); for (int I = 0; I < M; iTunes +) {int s = sc.nextInt (); int e = sc.nextInt () Int v = sc.nextInt (); new Line (smeme _ e _ (v)); lines.add (new Line (s _ r _ e _ e _ r v));} / / sort Collections.sort (lines); / / check and merge init () For (int I = 0; I < lines.size (); lines.get +) {if (! isConntect (lines.get (I) .s, lines.get (I) .e) {union (lines.get (I) .s, lines.get (I) .e); maxLine = lines.get (I) .v / / the last time must be the maximum length visited [I] = true;}} / / calculate the number int count = Nmuri 1; System.out.println (maxLine); System.out.println (count) For (int I = 0; I < lines.size (); iTunes +) {if (visited.s + "+ lines.get (I) .e);} sc.close () } / * point the root node of b to the root node of a * @ param a * @ param b * / private static void union (int a, int b) {int tempA = SET [a]; int tempB = SET [b] If (tempANGUSTABSTABE) {SET [tempB] = tempA }} / * * whether param b connects * @ param a * @ param b * @ return * / private static boolean isConntect (int a, int b) {return find (a) = find (b) } / * returns the root node of a * @ param b * @ return * / private static int find (int a) {if (set [a] = = a) {return a } else {SET [a] = find (set [a]); return SET [a];}} / * and look up set initialization * / private static void init () {for (int I = 1; I < SET.length) Static class Line implements Comparable +) {SET [I] = I;}} static class Line implements Comparable {public int s; public int e; public int v Public Line (int s, int e, int v) {this.s = s; this.e = e; this.v = v } @ Override public int compareTo (Line o) {if (this.v! = o.v) {return this.v-o.v;} else {return this.s-o.s Thank you for reading, the above is the content of "how to achieve the minimum spanning tree in java kruskal". After the study of this article, I believe you have a deeper understanding of how to achieve the minimum spanning tree in java kruskal, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report