-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path348.go
90 lines (78 loc) · 1.42 KB
/
348.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
89
90
// UVa 348 - Optimal Array Multiplication Sequence
package main
import (
"fmt"
"io"
"math"
"os"
)
var (
m [][2]int
c [][][2]int
)
func makeCache(n int) {
c = make([][][2]int, n)
for i := range c {
c[i] = make([][2]int, n)
for j := range c[i] {
c[i][j][0] = math.MaxInt32
}
}
}
// |min(c[i,k]+c[k+1,j]+m[i].rows*m[k].cols*m[j].cols, k∈[i,j)), if j>i
//c[i,j]=|
// |0, if j=i
func dp(i, j int) int {
if c[i][j][0] != math.MaxInt32 {
return c[i][j][0]
}
if i == j {
c[i][j][0] = 0
return 0
}
sum := math.MaxInt32
pos := 0
for k := i; k < j; k++ {
left := dp(i, k)
right := dp(k+1, j)
cost := m[i][0] * m[k][1] * m[j][1]
if sum > left+right+cost {
sum = left + right + cost
pos = k
}
}
c[i][j][0], c[i][j][1] = sum, pos
return c[i][j][0]
}
func output(out io.Writer, i, j int) {
if i == j {
fmt.Fprintf(out, "A%d", i+1)
} else {
fmt.Fprint(out, "(")
output(out, i, c[i][j][1])
fmt.Fprint(out, " x ")
output(out, c[i][j][1]+1, j)
fmt.Fprint(out, ")")
}
}
func main() {
in, _ := os.Open("348.in")
defer in.Close()
out, _ := os.Create("348.out")
defer out.Close()
var n int
for kase := 1; ; kase++ {
if fmt.Fscanf(in, "%d", &n); n == 0 {
break
}
m = make([][2]int, n)
for i := range m {
fmt.Fscanf(in, "%d%d", &m[i][0], &m[i][1])
}
makeCache(n)
dp(0, n-1)
fmt.Fprintf(out, "Case %d: ", kase)
output(out, 0, n-1)
fmt.Fprintln(out)
}
}