-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path117.go
88 lines (79 loc) · 1.55 KB
/
117.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
82
83
84
85
86
87
88
// UVa 117 - The Postal Worker Rings Once
package main
import (
"fmt"
"math"
"os"
)
type edge struct {
n1, n2 string
l int
}
func toMatrix(edges []edge) ([]int, int, [][]int) {
ns := make(map[string]int)
nl := make(map[string]int)
idx, sum := 0, 0
for _, v := range edges {
sum += v.l
ns[v.n1]++
ns[v.n2]++
if _, ok := nl[v.n1]; !ok {
nl[v.n1] = idx
idx++
}
if _, ok := nl[v.n2]; !ok {
nl[v.n2] = idx
idx++
}
}
matrix := make([][]int, len(nl))
for i := range matrix {
matrix[i] = make([]int, len(nl))
for j := range matrix[i] {
matrix[i][j] = math.MaxInt32
}
}
for _, v := range edges {
matrix[nl[v.n1]][nl[v.n2]], matrix[nl[v.n2]][nl[v.n1]] = v.l, v.l
}
var odds []int
for k, v := range ns {
if v%2 != 0 {
odds = append(odds, nl[k])
}
}
return odds, sum, matrix
}
func floydWarshall(matrix [][]int) [][]int {
for k := range matrix {
for i := range matrix {
for j := range matrix {
matrix[i][j] = min(matrix[i][j], matrix[i][k]+matrix[k][j])
}
}
}
return matrix
}
func main() {
in, _ := os.Open("117.in")
defer in.Close()
out, _ := os.Create("117.out")
defer out.Close()
var word string
var edges []edge
for {
if _, err := fmt.Fscanf(in, "%s", &word); err != nil {
break
}
if word != "deadend" {
edges = append(edges, edge{word[:1], word[len(word)-1:], len(word)})
} else {
if odds, sum, matrix := toMatrix(edges); len(odds) == 0 {
fmt.Fprintln(out, sum)
} else {
fmt.Fprintln(out, sum+floydWarshall(matrix)[odds[0]][odds[1]])
}
edges = nil
}
}
}