-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.java
41 lines (37 loc) · 1.08 KB
/
Solution.java
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
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;
}
}
}