Editorial for DMOPC '21 Contest 9 P6 - Shortest Paths
Submitting an official solution before solving the problem yourself is a bannable offence.
Authors:
,Hint
Think about BFS order.
Hint 2
Try some DP.
Subtask 1
As in the hint, consider the "layers" formed by processing the nodes in the graph in BFS order from . For this subtask, let's try all possible layers. Given a bitmask, we can make a mapping to layers by considering the s as "separators". There are possible arrangements, corresponding to ways to choose some of the positions to increase the distance by , where position must be chosen (so that position , the source, is the only node with a distance of ), and we can brute force all of them.
We can add up the distances from choosing a node as node and rearranging the rest of the nodes with some combinatorics, so it suffices to count the number of possible graphs if the nodes in these layers are fixed. Note that we only need all edges to be either within the same layer or cross between two adjacent layers, and each node much have at least one edge to its previous layer.
We can use dynamic programming, with a state similar to (number of nodes processed so far, number of edges used so far), where the transitions consider all possibilities for the number of edges the current node uses (using some more combinatorics to count the number of ways to arrange them).
Time Complexity: , or with slower polynomial factors.
Subtask 2
For this subtask, let's do some different DP. Use a state of (number of nodes processed so far, number of edges used so far, number of nodes in the last layer). We must also be careful about where is, so let's store three numbers per state. One, the number of paths. Two, the sum of the path lengths. Three, assuming node has been placed, the sum of path lengths. We need not store the number of paths if has been placed, since the only reason we store the number of paths in the first place is so that we can maintain the sum of lengths.
When transitioning, consider all possible next layer sizes, and new edges. We then need to calculate the number of ways to 1) connect all new nodes to the previous layer and 2) add the remaining edges, possibly with some between edges in this layer.
To do this efficiently, we can precompute using some DP. For the DP, the state is (previous layer nodes, new layer nodes, edges available). We transition by either creating a new layer, or building on the current layer. Full details are left as an exercise.
Time Complexity: , or possibly .
Hint
What if you used a generating function, or polynomial?
Subtask 3
Note that the number of edges is actually irrelevant to our state, that is, although we must store it, it does not factor into our transitions at all.
We can change our state to (total nodes, nodes in last layer), and store a polynomial (some may know it as a generating function). Why would we do this?
Imagine adding nodes to our layer one at a time. Let be the current size of the layer and be the size of the previous layer. Then our polynomial is multiplied by:
We need to connect at least one previous layer node to the new node, and we can have up to , so we have . We can optionally have up to between-layer edges, so we have .
Using this generating function, we can speed up our transitions drastically.
Time Complexity:
Hint
We have polynomials in base . Might another base be better?
Subtask 4
We should use a base of instead of for our polynomial. This way instead of an multiplication at each step, we get .
Specifically, our polynomial before was a vector of coefficients like:
Let's make it:
Notably, we must keep all terms of the polynomial, unlike the last subtask, since higher terms are not irrelevant anymore.
We can expand the polynomial at the end to get our final answer.
Time Complexity:
Comments