给出 graph
为有 N 个节点(编号为 0, 1, 2, ..., N-1
)的无向连通图。
graph.length = N
,且只有节点 i
和 j
连通时,j != i
在列表 graph[i]
中恰好出现一次。
返回能够访问所有节点的最短路径的长度。你可以在任一节点开始和停止,也可以多次重访节点,并且可以重用边。
示例 1:
输入:[[1,2,3],[0],[0],[0]] 输出:4 解释:一个可能的路径为 [1,0,2,0,3]
示例 2:
输入:[[1],[0,2,4],[1,3,4],[2],[1,2]] 输出:4 解释:一个可能的路径为 [0,1,4,2,3]
提示:
1 <= graph.length <= 12
0 <= graph[i].length < graph.length
因为每条边权值一样,所以用 BFS 就能得出最短路径,过程中可以用状态压缩记录节点的访问情况
class Solution:
def shortestPathLength(self, graph: List[List[int]]) -> int:
n = len(graph)
dst = -1 ^ (-1 << n)
q = deque()
vis = [[False] * (1 << n) for _ in range(n)]
for i in range(n):
q.append((i, 1 << i, 0))
vis[i][1 << i] = True
while q:
u, state, dis = q.popleft()
for v in graph[u]:
nxt = state | (1 << v)
if nxt == dst:
return dis + 1
if not vis[v][nxt]:
q.append((v, nxt, dis + 1))
vis[v][nxt] = True
return 0
class Solution {
public int shortestPathLength(int[][] graph) {
int n = graph.length;
int dst = -1 ^ (-1 << n);
Queue<Tuple> queue = new ArrayDeque<>();
boolean[][] vis = new boolean[n][1 << n];
for (int i = 0; i < n; i++) {
queue.offer(new Tuple(i, 1 << i, 0));
vis[i][1 << i] = true;
}
while (!queue.isEmpty()) {
Tuple t = queue.poll();
int u = t.u, state = t.state, dis = t.dis;
for (int v : graph[u]) {
int next = state | (1 << v);
if (next == dst) {
return dis + 1;
}
if (!vis[v][next]) {
queue.offer(new Tuple(v, next, dis + 1));
vis[v][next] = true;
}
}
}
return 0;
}
private static class Tuple {
int u;
int state;
int dis;
public Tuple(int u, int state, int dis) {
this.u = u;
this.state = state;
this.dis = dis;
}
}
}
type tuple struct {
u int
state int
dis int
}
func shortestPathLength(graph [][]int) int {
n := len(graph)
dst := -1 ^ (-1 << n)
q := make([]tuple, 0)
vis := make([][]bool, n)
for i := 0; i < n; i++ {
vis[i] = make([]bool, 1<<n)
q = append(q, tuple{i, 1 << i, 0})
vis[i][1<<i] = true
}
for len(q) > 0 {
t := q[0]
q = q[1:]
cur, state, dis := t.u, t.state, t.dis
for _, v := range graph[cur] {
next := state | (1 << v)
if next == dst {
return dis + 1
}
if !vis[v][next] {
q = append(q, tuple{v, next, dis + 1})
vis[v][next] = true
}
}
}
return 0
}