-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathShortest Path Visiting All Nodes.java
48 lines (45 loc) · 1.37 KB
/
Shortest Path Visiting All Nodes.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
42
43
44
45
46
47
48
// Runtime: 17 ms (Top 78.04%) | Memory: 46.7 MB (Top 69.44%)
class Solution {
class Pair {
int i;
int path;
public Pair(int i, int path) {
this.i = i;
this.path = path;
}
}
public int shortestPathLength(int[][] graph) {
/*
For each node currentNode, steps as key, visited as value
boolean[currentNode][steps]
*/
int n = graph.length;
// 111....1, 1<< n - 1
int allVisited = (1 << n) - 1;
boolean[][] visited = new boolean[n][1 << n];
Queue<Pair> q = new LinkedList<>();
for (int i = 0; i < n; i++) {
if (1 << i == allVisited) return 0;
visited[i][1 << i] = true;
q.offer(new Pair(i, 1 << i));
}
int step = 0;
while (!q.isEmpty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
Pair p = q.poll();
int[] edges = graph[p.i];
for(int t: edges) {
int path = p.path | (1 << t);
if (path == allVisited) return step + 1;
if (!visited[t][path]) {
visited[t][path] = true;
q.offer(new Pair(t, path));
}
}
}
step++;
}
return step;
}
}