-
Notifications
You must be signed in to change notification settings - Fork 2
/
minSpantree.go
81 lines (59 loc) · 1.43 KB
/
minSpantree.go
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
package main
import (
"fmt"
"bufio"
"os"
"strconv"
"sort"
)
type Vertex struct {
value int
parent *Vertex
}
func (v *Vertex) getRoot() *Vertex {
if v.parent == nil {
return v;
} else {
return v.parent.getRoot()
}
}
type Edge struct {
u, v *Vertex
weight int
}
type Edges []Edge
// Implement the sort.Interface for Edges
func (edges Edges) Len() int { return len(edges) }
func (edges Edges) Less(i, j int) bool { return edges[i].weight < edges[j].weight }
func (edges Edges) Swap(i, j int) { edges[i], edges[j] = edges[j], edges[i] }
func getNextInt(scanner *bufio.Scanner) int {
scanner.Scan()
i, _ := strconv.Atoi(scanner.Text());
return i;
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanWords)
for test := getNextInt(scanner); test > 0; test-- {
numVertices := getNextInt(scanner)
numEdges := getNextInt(scanner)
var vertices = make([]Vertex, numVertices)
var edges Edges
for i := 0; i < numEdges; i++ {
uValue := getNextInt(scanner) - 1
vValue := getNextInt(scanner) - 1
vertices[uValue].value = uValue
vertices[vValue].value = vValue
edges = append(edges, Edge{ &vertices[uValue], &vertices[vValue], getNextInt(scanner) })
}
sort.Sort(edges)
totalWeight := 0
for _, edge := range edges {
if edge.u.getRoot() != edge.v.getRoot() {
edge.v.getRoot().parent = edge.u.getRoot();
totalWeight += edge.weight;
}
}
fmt.Println(totalWeight)
}
}