D - Jumping Takahashi 2 Editorial by spheniscine


There are several workable solutions.

One solution is the use binary search over the final value of \(S\). For a fixed \(S = x\), construct a directed graph where the edge \((u, v)\) exists iff Takahashi can jump from \(u\) to \(v\), there may be up to \(O(N^2)\) such edges. If there exists a node that can reach all other nodes over one or more directed edges, \(S \leq x\). You may BFS/DFS from each node to solve in time \(O(N^3 \log A)\), where \(A\) is the maximum possible value of \(S\). Note that \(A = 4 \cdot 10^9\). It’s also possible to achieve \(O(N^2 \log A)\) by using strongly connected components and topological sorting, in which case you only have to start the BFS/DFS from an arbitrary node from the first SCC in the sorted order.

Another solution is to use either Dijkstra’s from each node for \(O(N^3 \log N)\), or the Floyd-Warshall algorithm for \(O(N^3)\). Note that the cost of a path is here defined as the maximum weight of an edge (where the weight of edge \((i, j)\) is \(\large\left\lceil\frac {|x_i - x_j| + |y_i - y_j|} {P_i}\right\rceil\)), rather than the sum of weights; these algorithms will still work with this modification. (Dijkstra’s because partial solutions are still non-decreasing in cost, Floyd-Warshall because \(\max\) is associative and distributes over \(\min\), just as \(+\) would)

posted:
last update: