Problem link : this problem has made me scratch my head a lot .
The ideas i have tried and failed
1.Multisource bfs : can't find a way to get distances of a shop to other anime shops ,all non shop nodes are fine.
2.Tried finding if a node can be reached by two different shops ,but failed as my method leads to TLE and also if there are two cycles beginning from an anime shop and includes a node both times, then it failed my method.
3.To eliminate some sections ,used DSU to find components having only one anime shop , and solving them is easy , then proceeded to find MST of the graph by intuition ,but now i can't find what to do with MST and stuck here due to some cases like if there are two paths from one anime shop to other and one of them is direct connection, other is through a node in between them and say even if i found MST , what would I even do of that?
So these were my ideas, and i am done for so please give some hints to make me think in right direction like a topic required to solve it or something.








Multi-source bfs idea is good. But to make it work for shop nodes, in addition to the distance to the nearest shop, you also need to memorize what the nearest shop is. So you would have O(n^2) states: dist[v][shop]. Are all these states actually needed? Can you reduce the number of states?
If there are already two shops that can reach a node, and now a third shop reaches that node. Is this third shop ever useful?
No! A third shop is never useful. So you only need two states for each node. dist[v][1] and dist[v][2] and you need to remember what shop they came from, so also keep track of shop[v][1] and shop[v][2].
Read all hints first. Initialize all dist[v][1] = dist[v][2] = INF for all nodes. Then set dist[v][1] = 0, shop[v][1] = v, for all shops v, and insert the pairs (v, 1) into a queue. Then do a bfs. For a pair (v, 1) update all neighbors u of v, such that
For a pair (v, 2) update all neighbors u of v, such that
Now you can find the nearest shop from every node by considering dist[v][1] (only if shop[v][1] != v) and dist[v][2] (here you don't need to check, because one of the two shops is guaranteed to be different from v)
Thanks,I finally got it now.
All problem's solutions are available ig, on ykw site
Which site are you talking about?