-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10986.go
58 lines (52 loc) · 1.28 KB
/
10986.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
// UVa 10986 - Sending email
package main
import (
"fmt"
"math"
"os"
)
type node struct{ server, latency int }
func spfa(n, s, t int, matrix [][]node) int {
distance := make([]int, n)
for i := range distance {
distance[i] = math.MaxInt32
}
distance[s] = 0
visited := map[int]bool{s: true}
for queue := []int{s}; len(queue) > 0; queue = queue[1:] {
curr := queue[0]
for _, to := range matrix[curr] {
if distance[to.server] > distance[curr]+to.latency {
distance[to.server] = distance[curr] + to.latency
if !visited[to.server] {
queue = append(queue, to.server)
visited[to.server] = true
}
}
}
}
return distance[t]
}
func main() {
in, _ := os.Open("10986.in")
defer in.Close()
out, _ := os.Create("10986.out")
defer out.Close()
var kase, m, s, t, s1, s2, l, n int
fmt.Fscanf(in, "%d", &kase)
for i := 1; i <= kase; i++ {
fmt.Fscanf(in, "%d%d%d%d", &n, &m, &s, &t)
matrix := make([][]node, n)
for ; m > 0; m-- {
fmt.Fscanf(in, "%d%d%d", &s1, &s2, &l)
matrix[s1] = append(matrix[s1], node{s2, l})
matrix[s2] = append(matrix[s2], node{s1, l})
}
fmt.Fprintf(out, "Case #%d: ", i)
if distance := spfa(n, s, t, matrix); distance == math.MaxInt32 {
fmt.Fprintln(out, "unreachable")
} else {
fmt.Fprintln(out, distance)
}
}
}