forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0847.go
29 lines (29 loc) · 785 Bytes
/
0847.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
func shortestPathLength(graph [][]int) int {
n := uint(len(graph))
t, res := uint((1 << n) - 1), 0
q := make([][]uint, n)
mem := make([][]uint, n)
for i := uint(0); i < n; i++ {
mem[i] = make([]uint, 1 << n);
q[i] = []uint{i, 1 << i}
}
for true {
nq := len(q)
for i := 0; i < nq; i++ {
pre := q[0]
q = q[1:]
if pre[1] == t {
return res
}
for _, j := range graph[pre[0]] {
k, new_stat := uint(j), pre[1] | 1 << uint(j)
if mem[k][new_stat] == 0 {
q = append(q, []uint{k, new_stat})
mem[k][new_stat] = 1
}
}
}
res++
}
return 0
}