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.

Author: Kirito

We can construct a graph with N\times(K+1) vertices and KM vertices. Intuitively, the vertex (v, k) corresponds to reaching vertex k having bought items 1, 2, \ldots, k.

To construct the graph, we add the directed edge (u,k)\to(v,k) if and only if uv is an edge of the input, and store v does not sell item k+1. Otherwise, if uv is an edge of the input, and store v does sell item k+1, we add the edge (u,k)\to(v,k+1) instead.

Finally, running a multisource BFS, we return the shortest distance to any vertex (v,K).


Comments

There are no comments at the moment.