-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshortestPathVisitingAllNodes.cpp
More file actions
34 lines (33 loc) · 1.01 KB
/
shortestPathVisitingAllNodes.cpp
File metadata and controls
34 lines (33 loc) · 1.01 KB
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
// Source: https://leetcode.com/problems/shortest-path-visiting-all-nodes/
// Author: Miao Zhang
// Date: 2021-03-17
class Solution {
public:
int shortestPathLength(vector<vector<int>>& graph) {
int n = graph.size();
int finalstate = (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 step = 0;
while (!q.empty()) {
int size = q.size();
while (size--) {
auto n = q.front();
q.pop();
int node = n.first;
int state = n.second;
if (state == finalstate) return step;
if (visited[node][state]) continue;
visited[node][state] = 1;
for (int nxt: graph[node]) {
q.push({nxt, state | 1 << nxt});
}
}
step++;
}
return -1;
}
};