We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The key to solving the Johnland problem is to realize that the shortest paths between cities will always lie within a minimum spanning tree (MST), since the edge weights are distinct powers of two. Once you construct the MST, each edge can be seen as a “bridge” that splits the graph into two groups. The contribution of that edge to the total distance is its weight multiplied by the number of node pairs it separates, and by summing these contributions you get the overall result. Finally, the answer is expressed in binary format. Visit site For a deeper dive into this concept and related problem-solving strategies.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Roads in HackerLand
You are viewing a single comment's thread. Return to all comments →
The key to solving the Johnland problem is to realize that the shortest paths between cities will always lie within a minimum spanning tree (MST), since the edge weights are distinct powers of two. Once you construct the MST, each edge can be seen as a “bridge” that splits the graph into two groups. The contribution of that edge to the total distance is its weight multiplied by the number of node pairs it separates, and by summing these contributions you get the overall result. Finally, the answer is expressed in binary format. Visit site For a deeper dive into this concept and related problem-solving strategies.