-
Notifications
You must be signed in to change notification settings - Fork 2
/
minSpantree.java
89 lines (63 loc) · 2 KB
/
minSpantree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Vertex {
public int value;
public Vertex parent;
public Vertex(int value) {
this.value = value;
this.parent = this;
}
public Vertex(int value, Vertex parent) {
this.value = value;
this.parent = parent;
}
public Vertex getRoot() {
return this.parent == this ? this : this.parent.getRoot();
}
}
class Edge implements Comparable<Edge>{
public Vertex u, v;
public int weight;
Edge(Vertex u, Vertex v, int weight)
{
this.u = u;
this.v = v;
this.weight = weight;
}
public int compareTo(Edge e) {
return this.weight - e.weight;
}
}
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
for(int test = scanner.nextInt(); test > 0; test--) {
int numVertices = scanner.nextInt();
int numEdges = scanner.nextInt();
Vertex[] vertices = new Vertex[numVertices];
ArrayList<Edge> edges = new ArrayList<Edge>();
for(int i = 0; i < numEdges; ++i) {
int uValue = scanner.nextInt() - 1;
int vValue = scanner.nextInt() - 1;
if (vertices[uValue] == null) {
vertices[uValue] = new Vertex(uValue);
}
if (vertices[vValue] == null) {
vertices[vValue] = new Vertex(vValue);
}
edges.add( new Edge( vertices[uValue], vertices[vValue], scanner.nextInt() ) );
}
Collections.sort(edges);
int totalWeight = 0;
for (Edge edge : edges) {
if (edge.u.getRoot() != edge.v.getRoot()) {
edge.v.getRoot().parent = edge.u.getRoot();
totalWeight += edge.weight;
}
}
System.out.println(totalWeight);
//
}
}
}