-
Notifications
You must be signed in to change notification settings - Fork 481
/
0847.cpp
31 lines (31 loc) · 864 Bytes
/
0847.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
class Solution
{
public:
int shortestPathLength(vector<vector<int>>& graph)
{
int n = graph.size();
int t = (1 << n) - 1, res = 0;
int mem[n][1 << n] = {};
queue<pair<int, int>> q;
for (int i = 0; i < n; i++) q.push({i, 1 << i});
while (true)
{
int nq = q.size();
for (int i = 0; i < nq; i++)
{
auto pre = q.front(); q.pop();
if (pre.second == t) return res;
for (auto j : graph[pre.first])
{
int new_stat = pre.second | 1 << j;
if (!mem[j][new_stat])
{
q.push({j, new_stat});
mem[j][new_stat] = 1;
}
}
}
res++;
}
}
};