-
Notifications
You must be signed in to change notification settings - Fork 27
/
10048.go
57 lines (52 loc) · 1.08 KB
/
10048.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
// UVa 10048 - Audiophobia
package main
import (
"fmt"
"math"
"os"
)
func floydWarshall(matrix [][]int) {
for k := range matrix {
for i := range matrix {
for j := range matrix {
matrix[i][j] = min(matrix[i][j], max(matrix[i][k], matrix[k][j]))
}
}
}
}
func main() {
in, _ := os.Open("10048.in")
defer in.Close()
out, _ := os.Create("10048.out")
defer out.Close()
var c, s, q, c1, c2, d int
for kase := 1; ; kase++ {
if fmt.Fscanf(in, "%d%d%d", &c, &s, &q); c == 0 && s == 0 && q == 0 {
break
}
matrix := make([][]int, c)
for i := range matrix {
matrix[i] = make([]int, c)
for j := range matrix[i] {
matrix[i][j] = math.MaxInt32
}
}
for ; s > 0; s-- {
fmt.Fscanf(in, "%d%d%d", &c1, &c2, &d)
matrix[c1-1][c2-1], matrix[c2-1][c1-1] = d, d
}
floydWarshall(matrix)
if kase > 1 {
fmt.Fprintln(out)
}
fmt.Fprintf(out, "Case #%d\n", kase)
for ; q > 0; q-- {
fmt.Fscanf(in, "%d%d", &c1, &c2)
if matrix[c1-1][c2-1] == math.MaxInt32 {
fmt.Fprintln(out, "no path")
} else {
fmt.Fprintln(out, matrix[c1-1][c2-1])
}
}
}
}