公式

B - Stones on Grid 解説 by evima


Placing a stone on cell \((x_i, y_i)\) corresponds to adding a directed edge \((x_i, y_i)\) in a graph with \(N\) vertices.

Then, the goal state is “for every vertex, the in-degree equals the out-degree”.

Under the condition that directed edge \((x_1, y_1)\) is added, we need to find the cycle containing it with the minimum sum of weights, which is equivalent to finding the shortest path from vertex \(y_1\) to vertex \(x_1\), which can be solved using Dijkstra’s algorithm.

Thus, this problem can be solved in \(O(N+M\log M)\).

投稿日時:
最終更新: