Skip to content

Commit e2d8ac6

Browse files
committed
feat: add solutions to lc problem: No.0847.Shortest Path Visiting All Nodes
1 parent 4c4b4bb commit e2d8ac6

File tree

5 files changed

+307
-2
lines changed

5 files changed

+307
-2
lines changed

solution/0800-0899/0847.Shortest Path Visiting All Nodes/README.md

+103-1
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,124 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
因为每条边权值一样,所以用 BFS 就能得出最短路径,过程中可以用状态压缩记录节点的访问情况
48+
4749
<!-- tabs:start -->
4850

4951
### **Python3**
5052

5153
<!-- 这里可写当前语言的特殊实现逻辑 -->
5254

5355
```python
54-
56+
class Solution:
57+
def shortestPathLength(self, graph: List[List[int]]) -> int:
58+
n = len(graph)
59+
dst = -1 ^ (-1 << n)
60+
61+
q = deque()
62+
vis = [[False] * (1 << n) for _ in range(n)]
63+
for i in range(n):
64+
q.append((i, 1 << i, 0))
65+
vis[i][1 << i] = True
66+
67+
while q:
68+
u, state, dis = q.popleft()
69+
for v in graph[u]:
70+
nxt = state | (1 << v)
71+
if nxt == dst:
72+
return dis + 1
73+
if not vis[v][nxt]:
74+
q.append((v, nxt, dis + 1))
75+
vis[v][nxt] = True
76+
return 0
5577
```
5678

5779
### **Java**
5880

5981
<!-- 这里可写当前语言的特殊实现逻辑 -->
6082

6183
```java
84+
class Solution {
85+
public int shortestPathLength(int[][] graph) {
86+
int n = graph.length;
87+
int dst = -1 ^ (-1 << n);
88+
89+
Queue<Tuple> queue = new ArrayDeque<>();
90+
boolean[][] vis = new boolean[n][1 << n];
91+
for (int i = 0; i < n; i++) {
92+
queue.offer(new Tuple(i, 1 << i, 0));
93+
vis[i][1 << i] = true;
94+
}
95+
96+
while (!queue.isEmpty()) {
97+
Tuple t = queue.poll();
98+
int u = t.u, state = t.state, dis = t.dis;
99+
for (int v : graph[u]) {
100+
int next = state | (1 << v);
101+
if (next == dst) {
102+
return dis + 1;
103+
}
104+
if (!vis[v][next]) {
105+
queue.offer(new Tuple(v, next, dis + 1));
106+
vis[v][next] = true;
107+
}
108+
}
109+
}
110+
return 0;
111+
}
112+
113+
private static class Tuple {
114+
int u;
115+
int state;
116+
int dis;
117+
118+
public Tuple(int u, int state, int dis) {
119+
this.u = u;
120+
this.state = state;
121+
this.dis = dis;
122+
}
123+
}
124+
}
125+
```
62126

127+
### **Go**
128+
129+
```go
130+
type tuple struct {
131+
u int
132+
state int
133+
dis int
134+
}
135+
136+
func shortestPathLength(graph [][]int) int {
137+
n := len(graph)
138+
dst := -1 ^ (-1 << n)
139+
140+
q := make([]tuple, 0)
141+
vis := make([][]bool, n)
142+
for i := 0; i < n; i++ {
143+
vis[i] = make([]bool, 1<<n)
144+
q = append(q, tuple{i, 1 << i, 0})
145+
vis[i][1<<i] = true
146+
}
147+
148+
for len(q) > 0 {
149+
t := q[0]
150+
q = q[1:]
151+
cur, state, dis := t.u, t.state, t.dis
152+
for _, v := range graph[cur] {
153+
next := state | (1 << v)
154+
if next == dst {
155+
return dis + 1
156+
}
157+
if !vis[v][next] {
158+
q = append(q, tuple{v, next, dis + 1})
159+
vis[v][next] = true
160+
}
161+
}
162+
}
163+
return 0
164+
}
63165
```
64166

65167
### **...**

solution/0800-0899/0847.Shortest Path Visiting All Nodes/README_EN.md

+103-1
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,120 @@
7373

7474
## Solutions
7575

76+
Because each edge has the same weight, the shortest path can be solution by using BFS, and the access of the point can be recorded by state compression.
77+
7678
<!-- tabs:start -->
7779

7880
### **Python3**
7981

8082
```python
81-
83+
class Solution:
84+
def shortestPathLength(self, graph: List[List[int]]) -> int:
85+
n = len(graph)
86+
dst = -1 ^ (-1 << n)
87+
88+
q = deque()
89+
vis = [[False] * (1 << n) for _ in range(n)]
90+
for i in range(n):
91+
q.append((i, 1 << i, 0))
92+
vis[i][1 << i] = True
93+
94+
while q:
95+
u, state, dis = q.popleft()
96+
for v in graph[u]:
97+
nxt = state | (1 << v)
98+
if nxt == dst:
99+
return dis + 1
100+
if not vis[v][nxt]:
101+
q.append((v, nxt, dis + 1))
102+
vis[v][nxt] = True
103+
return 0
82104
```
83105

84106
### **Java**
85107

86108
```java
109+
class Solution {
110+
public int shortestPathLength(int[][] graph) {
111+
int n = graph.length;
112+
int dst = -1 ^ (-1 << n);
113+
114+
Queue<Tuple> queue = new ArrayDeque<>();
115+
boolean[][] vis = new boolean[n][1 << n];
116+
for (int i = 0; i < n; i++) {
117+
queue.offer(new Tuple(i, 1 << i, 0));
118+
vis[i][1 << i] = true;
119+
}
120+
121+
while (!queue.isEmpty()) {
122+
Tuple t = queue.poll();
123+
int u = t.u, state = t.state, dis = t.dis;
124+
for (int v : graph[u]) {
125+
int next = state | (1 << v);
126+
if (next == dst) {
127+
return dis + 1;
128+
}
129+
if (!vis[v][next]) {
130+
queue.offer(new Tuple(v, next, dis + 1));
131+
vis[v][next] = true;
132+
}
133+
}
134+
}
135+
return 0;
136+
}
137+
138+
private static class Tuple {
139+
int u;
140+
int state;
141+
int dis;
142+
143+
public Tuple(int u, int state, int dis) {
144+
this.u = u;
145+
this.state = state;
146+
this.dis = dis;
147+
}
148+
}
149+
}
150+
```
87151

152+
### **Go**
153+
154+
```go
155+
type tuple struct {
156+
u int
157+
state int
158+
dis int
159+
}
160+
161+
func shortestPathLength(graph [][]int) int {
162+
n := len(graph)
163+
dst := -1 ^ (-1 << n)
164+
165+
q := make([]tuple, 0)
166+
vis := make([][]bool, n)
167+
for i := 0; i < n; i++ {
168+
vis[i] = make([]bool, 1<<n)
169+
q = append(q, tuple{i, 1 << i, 0})
170+
vis[i][1<<i] = true
171+
}
172+
173+
for len(q) > 0 {
174+
t := q[0]
175+
q = q[1:]
176+
cur, state, dis := t.u, t.state, t.dis
177+
for _, v := range graph[cur] {
178+
next := state | (1 << v)
179+
if next == dst {
180+
return dis + 1
181+
}
182+
if !vis[v][next] {
183+
q = append(q, tuple{v, next, dis + 1})
184+
vis[v][next] = true
185+
}
186+
}
187+
}
188+
return 0
189+
}
88190
```
89191

90192
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
type tuple struct {
2+
u int
3+
state int
4+
dis int
5+
}
6+
7+
func shortestPathLength(graph [][]int) int {
8+
n := len(graph)
9+
dst := -1 ^ (-1 << n)
10+
11+
q := make([]tuple, 0)
12+
vis := make([][]bool, n)
13+
for i := 0; i < n; i++ {
14+
vis[i] = make([]bool, 1<<n)
15+
q = append(q, tuple{i, 1 << i, 0})
16+
vis[i][1<<i] = true
17+
}
18+
19+
for len(q) > 0 {
20+
t := q[0]
21+
q = q[1:]
22+
cur, state, dis := t.u, t.state, t.dis
23+
for _, v := range graph[cur] {
24+
next := state | (1 << v)
25+
if next == dst {
26+
return dis + 1
27+
}
28+
if !vis[v][next] {
29+
q = append(q, tuple{v, next, dis + 1})
30+
vis[v][next] = true
31+
}
32+
}
33+
}
34+
return 0
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
public int shortestPathLength(int[][] graph) {
3+
int n = graph.length;
4+
int dst = -1 ^ (-1 << n);
5+
6+
Queue<Tuple> queue = new ArrayDeque<>();
7+
boolean[][] vis = new boolean[n][1 << n];
8+
for (int i = 0; i < n; i++) {
9+
queue.offer(new Tuple(i, 1 << i, 0));
10+
vis[i][1 << i] = true;
11+
}
12+
13+
while (!queue.isEmpty()) {
14+
Tuple t = queue.poll();
15+
int u = t.u, state = t.state, dis = t.dis;
16+
for (int v : graph[u]) {
17+
int next = state | (1 << v);
18+
if (next == dst) {
19+
return dis + 1;
20+
}
21+
if (!vis[v][next]) {
22+
queue.offer(new Tuple(v, next, dis + 1));
23+
vis[v][next] = true;
24+
}
25+
}
26+
}
27+
return 0;
28+
}
29+
30+
private static class Tuple {
31+
int u;
32+
int state;
33+
int dis;
34+
35+
public Tuple(int u, int state, int dis) {
36+
this.u = u;
37+
this.state = state;
38+
this.dis = dis;
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import List
2+
from collections import deque
3+
4+
5+
class Solution:
6+
def shortestPathLength(self, graph: List[List[int]]) -> int:
7+
n = len(graph)
8+
dst = -1 ^ (-1 << n)
9+
10+
q = deque()
11+
vis = [[False] * (1 << n) for _ in range(n)]
12+
for i in range(n):
13+
q.append((i, 1 << i, 0))
14+
vis[i][1 << i] = True
15+
16+
while q:
17+
u, state, dis = q.popleft()
18+
for v in graph[u]:
19+
nxt = state | (1 << v)
20+
if nxt == dst:
21+
return dis + 1
22+
if not vis[v][nxt]:
23+
q.append((v, nxt, dis + 1))
24+
vis[v][nxt] = True
25+
return 0

0 commit comments

Comments
 (0)