-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprim.go
More file actions
109 lines (93 loc) · 1.92 KB
/
prim.go
File metadata and controls
109 lines (93 loc) · 1.92 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"bufio"
"container/heap"
"fmt"
"os"
"strconv"
)
type Edge struct {
a int
u *Node
}
type Node struct {
index,
key int
value *Node
adj []*Edge
}
type PriorityQueue []*Node
func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool { return pq[i].key < pq[j].key }
func (pq PriorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = i
pq[j].index = j
}
func (pq *PriorityQueue) Push(x any) {
n := len(*pq)
node := x.(*Node)
node.index = n
*pq = append(*pq, node)
}
func (pq *PriorityQueue) Pop() any {
old := *pq
n := len(old)
node := old[n-1]
old[n-1] = nil
node.index = -2
*pq = old[:n-1]
return node
}
func (pq *PriorityQueue) update(node *Node, value *Node, key int) {
node.value = value
node.key = key
heap.Fix(pq, node.index)
}
func prim(graph []*Node) int {
pq := make(PriorityQueue, 1)
v := graph[0]
pq[0] = v
heap.Init(&pq)
dist := 0
for {
for _, edge := range v.adj {
if edge.u.index == -1 {
edge.u.key = edge.a
edge.u.value = v
heap.Push(&pq, edge.u)
} else if edge.u.index != -2 && edge.a < edge.u.key {
pq.update(edge.u, v, edge.a)
}
}
if pq.Len() == 0 {
break
}
v = heap.Pop(&pq).(*Node)
dist += v.key
}
return dist
}
func scanInt(scanner *bufio.Scanner) int {
scanner.Scan()
n, _ := strconv.Atoi(scanner.Text())
return n
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanWords)
n, m := scanInt(scanner), scanInt(scanner)
graph := make([]*Node, n)
for i := 0; i < n; i++ {
graph[i] = &Node{-1, 0, nil, make([]*Edge, 0)}
}
for i := 0; i < m; i++ {
u, v, nel := scanInt(scanner), scanInt(scanner), scanInt(scanner)
graph[u].adj = append(graph[u].adj, &Edge{nel, graph[v]})
graph[v].adj = append(graph[v].adj, &Edge{nel, graph[u]})
}
dist := prim(graph)
writer := bufio.NewWriter(os.Stdout)
fmt.Fprintln(writer, dist)
writer.Flush()
}