Skip to content

P5304

We need to find the minimum distance between any of the given \(k\) nodes.

Step 1

We can consider a multi-source framework + meet in the middle. We can give each of the \(k\) nodes a distinct color, then run Dijkstra on it. Once it meets with a colored node, we can know that it is the smallest.

Step 2

But the graph is directed. We want to flip the direction of the edges after the midpoint. We can run Dijkstra on the original graph as well as on the graph with all edges inverted. If we iterate over all edges to find we they meet, we can find the minimum. However, we might accidentally do A → B → A, so we need to record the second max just in case.

Binary Decomposition Solution

For any answer pair \((u,v)\), there is at least 1 digit difference in \(u\) and \(v\). We iterate over all the digits, and classify them based on the digit into 2 sets. We then run Dijkstra on these 2 sets.