-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
number-of-possible-sets-of-closing-branches.cpp
124 lines (117 loc) · 4.14 KB
/
number-of-possible-sets-of-closing-branches.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Time: O(r + 2^n * n^2)
// Space: O(n^3)
// graph, bitmasks, Floyd-Warshall algorithm, backtracking
class Solution {
public:
int numberOfSets(int n, int maxDistance, vector<vector<int>>& roads) {
static const int INF = numeric_limits<int>::max();
const auto& check = [&](int mask, const auto& dist) {
for (int i = 0; i < n; ++i) {
if ((mask & (1 << i)) == 0) {
continue;
}
for (int j = i + 1; j < n; ++j) {
if ((mask & (1 << j)) == 0) {
continue;
}
if (dist[i][j] > maxDistance) {
return false;
}
}
}
return true;
};
const auto& floydWarshall = [&](auto& dist, int k) {
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (dist[i][k] != INF && dist[k][j] != INF) {
dist[j][i] = dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
};
int result = 0;
const function<void (int, int, vector<vector<int>>)> backtracking = [&](int i, int mask, vector<vector<int>> dist) {
if (i == n) {
result += check(mask, dist);
return;
}
for (int j = 0; j < 2; ++j) {
vector<vector<int>> new_dist(dist);
if (j) {
floydWarshall(new_dist, i);
}
backtracking(i + 1, mask | (j << i), new_dist);
}
};
vector<vector<int>> dist(n, vector<int>(n, INF));
for (int u = 0; u < n; ++u) {
dist[u][u] = 0;
}
for (const auto& r : roads) {
dist[r[0]][r[1]] = min(dist[r[0]][r[1]], r[2]);
dist[r[1]][r[0]] = min(dist[r[1]][r[0]], r[2]);
}
backtracking(0, 0, dist);
return result;
}
};
// Time: O(r + 2^n * n^2)
// Space: O(n^3)
// bitmasks, Floyd-Warshall algorithm, backtracking
class Solution2 {
public:
int numberOfSets(int n, int maxDistance, vector<vector<int>>& roads) {
static const int INF = numeric_limits<int>::max();
const auto& check = [&](int mask, const auto& dist) {
for (int i = 0; i < n; ++i) {
if ((mask & (1 << i)) == 0) {
continue;
}
for (int j = i + 1; j < n; ++j) {
if ((mask & (1 << j)) == 0) {
continue;
}
if (dist[i][j] > maxDistance) {
return false;
}
}
}
return true;
};
const auto& floydWarshall = [&](int mask, auto dist) {
for (int k = 0; k < n; ++k) {
if ((mask & (1 << k)) == 0) {
continue;
}
for (int i = 0; i < n; ++i) {
if ((mask & (1 << i)) == 0) { // optional, to speed up performance
continue;
}
for (int j = i + 1; j < n; ++j) {
if ((mask & (1 << j)) == 0) { // optional, to speed up performance
continue;
}
if (dist[i][k] != INF && dist[k][j] != INF) {
dist[j][i] = dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
}
return check(mask, dist);
};
vector<vector<int>> dist(n, vector<int>(n, INF));
for (int u = 0; u < n; ++u) {
dist[u][u] = 0;
}
for (const auto& r : roads) {
dist[r[0]][r[1]] = min(dist[r[0]][r[1]], r[2]);
dist[r[1]][r[0]] = min(dist[r[1]][r[0]], r[2]);
}
int result = 0;
for (int mask = 0; mask < (1 << n); ++mask) {
result += floydWarshall(mask, dist);
}
return result;
}
};