Editorial for CIW '26 P1 - Shopping Mall
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
We can construct a graph with vertices and
vertices.
Intuitively, the vertex
corresponds to reaching vertex
having bought items
.
To construct the graph, we add the directed edge if and only if
is an edge of the input, and store
does not sell item
.
Otherwise, if
is an edge of the input, and store
does sell item
, we add the edge
instead.
Finally, running a multisource BFS, we return the shortest distance to any vertex .
Comments