forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-cost-to-reach-city-with-discounts.cpp
48 lines (46 loc) · 1.74 KB
/
minimum-cost-to-reach-city-with-discounts.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
// Time: O((|E| + |V|) * log|V|) = O(|E| * log|V|) by using binary heap,
// if we can further to use Fibonacci heap, it would be O(|E| + |V| * log|V|)
// Space: O(|E| + |V|) = O(|E|)
class Solution {
public:
int minimumCost(int n, vector<vector<int>>& highways, int discounts) {
using P = pair<int, int>;
unordered_map<int, vector<P>> adj;
for (const auto& highway : highways) {
int u, v, w;
tie(u, v, w) = make_tuple(highway[0], highway[1], highway[2]);
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
unordered_map<int, unordered_map<int, int>> best;
best[0][discounts] = 0;
using T = tuple<int, int, int>;
priority_queue<T, vector<T>, greater<T>> min_heap;
min_heap.emplace(0, 0, discounts);
while (!empty(min_heap)) {
auto [total, u, k] = min_heap.top(); min_heap.pop();
if ((best.count(u) && best[u].count(k) && best[u][k] < total)) {
continue;
}
if (u == n - 1) {
return total;
}
for (const auto& [v, w] : adj[u]) {
if (!best.count(v) ||
!best[v].count(k) ||
total + w < best[v][k]) {
best[v][k] = total + w;
min_heap.emplace(total + w, v, k);
}
if (k > 0 &&
(!best.count(v) ||
!best[v].count(k - 1) ||
total + w / 2 < best[v][k - 1])) {
best[v][k - 1] = total + w / 2;
min_heap.emplace(total + w / 2, v, k - 1);
}
}
}
return -1;
}
};