-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathminimize-the-maximum-edge-weight-of-graph.cpp
129 lines (119 loc) · 4.27 KB
/
minimize-the-maximum-edge-weight-of-graph.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
125
126
127
128
129
// Time: O(nlogn + e)
// Space: O(n + e)
// dijkstra's algorithm
class Solution {
public:
int minMaxWeight(int n, vector<vector<int>>& edges, int threshold) {
static const int INF = numeric_limits<int>::max();
vector<unordered_map<int, int>> adj(n);
const auto& dijkstra = [&]() {
vector<int> best(size(adj), INF);
best[0] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> min_heap;
min_heap.emplace(0, 0);
while (!empty(min_heap)) {
const auto [curr, u] = min_heap.top(); min_heap.pop();
if (curr != best[u]) {
continue;
}
for (const auto& [v, w] : adj[u]) {
if (!(max(w, curr) < best[v])) {
continue;
}
best[v] = max(w, curr);
min_heap.emplace(best[v], v);
}
}
const int result = ranges::max(best);
return result != INF ? result : -1;
};
for (const auto& e : edges) {
adj[e[1]][e[0]] = adj[e[1]].count(e[0]) ? min(adj[e[1]][e[0]], e[2]) : e[2];
}
return dijkstra();
}
};
// Time: O(nlogn + e)
// Space: O(n + e)
// prim's algorithm
class Solution2 {
public:
int minMaxWeight(int n, vector<vector<int>>& edges, int threshold) {
static const int INF = numeric_limits<int>::max();
vector<unordered_map<int, int>> adj(n);
const auto& prim = [&]() {
vector<int> best(size(adj), INF);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> min_heap;
min_heap.emplace(0, 0);
while (!empty(min_heap)) {
const auto [curr, u] = min_heap.top(); min_heap.pop();
if (best[u] != INF) {
continue;
}
best[u] = curr;
for (const auto& [v, w] : adj[u]) {
if (best[v] != INF) {
continue;
}
min_heap.emplace(w, v);
}
}
const int result = ranges::max(best);
return result != INF ? result : -1;
};
for (const auto& e : edges) {
adj[e[1]][e[0]] = adj[e[1]].count(e[0]) ? min(adj[e[1]][e[0]], e[2]) : e[2];
}
return prim();
}
};
// Time: O(nlogw + e)
// Space: O(n + e)
// binary search, bfs
class Solution3 {
public:
int minMaxWeight(int n, vector<vector<int>>& edges, int threshold) {
static const int INF = numeric_limits<int>::max();
const auto& binary_search = [](auto left, auto right, const auto& check) {
while (left <= right) {
const auto mid = left + (right - left) / 2;
if (check(mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
};
vector<unordered_map<int, int>> adj(n);
const auto& check = [&](const auto& x) {
int cnt = size(adj);
vector<bool> lookup(size(adj));
lookup[0] = true;
--cnt;
vector<int> q = {0};
while (!empty(q)) {
vector<int> new_q;
for (const auto& u : q) {
for (const auto& [v, w] : adj[u]) {
if (w > x || lookup[v]) {
continue;
}
lookup[v] = true;
--cnt;
new_q.emplace_back(v);
}
}
q = move(new_q);
}
return cnt == 0;
};
for (const auto& e : edges) {
adj[e[1]][e[0]] = adj[e[1]].count(e[0]) ? min(adj[e[1]][e[0]], e[2]) : e[2];
}
const int left = ranges::min(edges, [](const auto& a, const auto& b) { return a[2] < b[2]; })[2];
const int right = ranges::max(edges, [](const auto& a, const auto& b) { return a[2] < b[2]; })[2];
const int result = binary_search(left, right, check);
return result <= right ? result : -1;
}
};