DMOJ · ahscc2p5

Arcadia Computing Contest 2 P5 - lp0 is on fire!

This C++ solution uses multi-source dijkstra for DMOJ ahscc2p5 Arcadia Computing Contest 2 P5 - lp0 is on fire!. Read the reasoning, inspect the code, or try your own test case below.

ahscc2p5Graphs & treesMulti-source DijkstraC++44 lines
Solution135of 248
Open official problem ↗ Download C++ file ↓ Search the library → Open full Code Lab ↗ Report an issue ↗

Approach

Multi-source Dijkstra

lp0 is on fire sprinkler graph and nearest-safe-distance computation match the multi-source Dijkstra.

Graphs & trees

Problem and code

Useful links.

Written by benbenyaojifen. Try the problem first, then compare your approach with the code.

Open official problem ↗View exact source file ↗
Implementation

P_5_lp_0_is_on_fire.cpp

C++

    #include <bits/stdc++.h>
    using namespace std;
    using ll = long long;
    using i128 = __int128;
    const int inf = 1e9;
    const ll INF = 2e18; //❄️
    int main() {
        ios::sync_with_stdio(0); cin.tie(0);
        int n, m, k; ll t; cin >> n >> m >> k >> t;
        vector<char> v(n + 1);
        for (int i = 0; i < k; i++) {
            int x; cin >> x;
            v[x] = 1;
        }
        vector<vector<pair<int, int>>> adj(n + 1);
        for (int i = 0; i < m; i++) {
            int u, v, w; cin >> u >> v >> w;
            adj[u].emplace_back(w, v);
            adj[v].emplace_back(w, u);
        }
        vector<ll> dist(n + 1, INF);
        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
        //muti source dijkstras
        for (int i = 1; i <= n; i++) {
            if (!v[i]) {
                pq.push({0, i});
                dist[i] = 0;
            }
        }
        while (!pq.empty()) {
            auto[w, u] = pq.top(); pq.pop();
            if (w > dist[u]) continue;
            for (auto[c, v] : adj[u]) {
                if (w + c < dist[v]) {
                    dist[v] = w + c;
                    pq.push({dist[v], v});
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            cout << (dist[i] <= t ? 0 : 1) << (i == n ? "\n" : "");
        }
        return 0;
    }
        

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 ↗Keep studying →

Test this problem

Run your code here.

Paste your code, run a test case, compare the output, or trace selected values.

Full trace, comparison & stress testing ↗
StatusReady
Output
No run yet.
Diagnostics
No diagnostics yet.

Each run is isolated and has strict limits. Passing one test does not guarantee the judge will accept the solution.