-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path11631.go
55 lines (48 loc) · 987 Bytes
/
11631.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
// UVa 11631 - Dark roads
package main
import (
"fmt"
"os"
"sort"
)
type road struct{ c1, c2, length int }
func unionFind(x int, f []int) int {
if f[x] != x {
f[x] = unionFind(f[x], f)
}
return f[x]
}
func solve(roads []road, n int) int {
sort.Slice(roads, func(i, j int) bool { return roads[i].length < roads[j].length })
f := make([]int, n)
for i := range f {
f[i] = i
}
var total int
for _, r := range roads {
if f1, f2 := unionFind(r.c1, f), unionFind(r.c2, f); f1 != f2 {
f[f1] = f2
total += r.length
}
}
return total
}
func main() {
in, _ := os.Open("11631.in")
defer in.Close()
out, _ := os.Create("11631.out")
defer out.Close()
var m, n int
for {
if fmt.Fscanf(in, "%d%d", &m, &n); m == 0 && n == 0 {
break
}
var total int
roads := make([]road, n)
for i := range roads {
fmt.Fscanf(in, "%d%d%d", &roads[i].c1, &roads[i].c2, &roads[i].length)
total += roads[i].length
}
fmt.Fprintln(out, total-solve(roads, n))
}
}