- Define the priority key and whether the smallest or largest item should lead.
- Push each candidate when it becomes eligible.
- Discard stale entries when necessary and process the best live candidate.
Code notes
- 47 lines of C++ from the credited upstream file 3112.cpp.
- The implementation visibly relies on sequence storage, work queue.
- 4 loop blocks detected.
Complexity
Count heap pushes and pops; each normally contributes a logarithmic factor in the heap size.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public:3 vector<int> minimumTime(int n, vector<vector<int>>& edges,4 vector<int>& disappear) {5 vector<vector<pair<int, int>>> graph(n);6 7 for (const vector<int>& edge : edges) {8 const int u = edge[0];9 const int v = edge[1];10 const int w = edge[2];11 graph[u].emplace_back(v, w);12 graph[v].emplace_back(u, w);13 }14 15 return dijkstra(graph, 0, disappear);16 }17 18 private:19 vector<int> dijkstra(const vector<vector<pair<int, int>>>& graph, int src,20 const vector<int>& disappear) {21 vector<int> dist(graph.size(), INT_MAX);22 23 dist[src] = 0;24 using P = pair<int, int>; 25 priority_queue<P, vector<P>, greater<>> minHeap;26 minHeap.emplace(dist[src], src);27 28 while (!minHeap.empty()) {29 const auto [d, u] = minHeap.top();30 minHeap.pop();31 if (d > dist[u])32 continue;33 for (const auto& [v, w] : graph[u])34 if (d + w < disappear[v] && d + w < dist[v]) {35 dist[v] = d + w;36 minHeap.push({dist[v], v});37 }38 }39 40 for (int& d : dist)41 if (d == INT_MAX)42 d = -1;43 44 return dist;45 }46};47