847 Shortest Path Visiting All Nodes¶
An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph.
graph.length = N, and j != i is in the list graph[i] exactly once, if and only if nodes i and j are connected.
Return the length of the shortest path that visits every node. You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges.
Example 1:
Example 2:
Solution BFS¶
Time complexity: O(n*2^n) Space complexity: O(n*2^n)
// Running time: 34 ms
class Solution {
public:
int shortestPathLength(vector<vector<int>>& graph) {
const int kAns = (1 << (graph.size())) - 1;
queue<pair<int, int>> q;
unordered_set<int> visited; // (cur_node << 16) | state
for (int i = 0; i < graph.size(); ++i)
q.push({i, 1 << i});
int steps = 0;
while (!q.empty()) {
int s = q.size();
while (s--) {
auto p = q.front();
q.pop();
int n = p.first;
int state = p.second;
if (state == kAns) return steps;
int key = (n << 16) | state;
if (visited.count(key)) continue;
visited.insert(key);
for (int next : graph[n])
q.push({next, state | (1 << next)});
}
++steps;
}
return -1;
}
};
vector
// Running time: 10 ms
class Solution {
public:
int shortestPathLength(vector<vector<int>>& graph) {
const int n = graph.size();
const int kAns = (1 << n) - 1;
queue<pair<int, int>> q;
vector<vector<int>> visited(n, vector<int>(1 << n));
for (int i = 0; i < n; ++i)
q.push({i, 1 << i});
int steps = 0;
while (!q.empty()) {
int s = q.size();
while (s--) {
auto p = q.front();
q.pop();
int node = p.first;
int state = p.second;
if (state == kAns) return steps;
if (visited[node][state]) continue;
visited[node][state] = 1;
for (int next : graph[node])
q.push({next, state | (1 << next)});
}
++steps;
}
return -1;
}
};