Problem solution · C++

CCC 2025 S4 - Floor is Lava

CCC 2025 S4 - Floor is Lava: a C++ solution using heap or priority queue. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Heap or priority queue
Source
CCCSolutions
Length
48 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Heap or priority queue

For CCC 2025 S4 - Floor is Lava, the implementation repeatedly takes the currently best candidate from a heap while inserting newly available choices.

  1. Define the priority key and whether the smallest or largest item should lead.
  2. Push each candidate when it becomes eligible.
  3. Discard stale entries when necessary and process the best live candidate.

Code notes

  • 48 lines of C++ from the credited upstream file ccc25s4.cpp.
  • The implementation visibly relies on sequence storage, work queue.
  • 3 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.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 2025 S4 - Floor is Lava · C++C++
Use this to learn the idea, then write your own version.
// Daniel Zhang, Pinetree Secondary// Dijkstra on edges instead, simpler implementation, harder to find #include <bits/stdc++.h> using namespace std; using ll = long long;using State = tuple<ll, ll, ll>; int vis[200001], edges[200001];vector<pair<ll, ll>> graph[200001]; int main() {    int n, m; cin >> n >> m;    for (int i = 0; i < m; i++) {        int u, v, w; cin >> u >> v >> w;        graph[u].push_back({v, i});        graph[v].push_back({u, i});        edges[i] = w;    }        priority_queue<State, vector<State>, greater<>> pq;    vector<ll> best(m + 1, LLONG_MAX);        pq.push({0, 1, m});        while (!pq.empty()) {        auto [c, a, i] = pq.top(); pq.pop();                if (vis[i]) continue;        vis[i] = 1;                if (a == n) {            cout << c;            return 0;        }                for (auto [b, j] : graph[a]) {            if (vis[j]) continue;            ll new_c = c + abs(edges[i] - edges[j]);            if (new_c < best[j]) {                best[j] = new_c;                pq.push({new_c, b, j});            }        }    }}

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗