-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathShortest Path Visiting All Nodes.cpp
41 lines (41 loc) · 1.11 KB
/
Shortest Path Visiting All Nodes.cpp
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(vector<vector<int>>& graph) {
int n = graph.size();
string mask = "";
string eq = "";
for(int i=0; i<n; i++){
mask += '0';
eq += '1';
}
queue<pair<int,string>>q;
set<pair<int,string>>s;
for(int i=0; i<n; i++){
string temp = mask;
temp[i] = '1';
q.push({i,temp});
s.insert({i,temp});
}
int c = 0;
int flag = 0;
while(!q.empty()){
int size = q.size();
for(int i=0; i<size; i++){
auto top = q.front();
q.pop();
if(top.second == eq) return c;
for(auto p: graph[top.first]){
string temp1 = top.second;
temp1[p] = '1';
if(s.count({p,temp1}) == 0){
q.push({p,temp1});
s.insert({p,temp1});
}
}
}
c++;
cout<<"c is "<<c;
}
return -1;
}
};