Editorial for COI '11 #1 Kampanja
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
The first step of the solution is computing , the minimum distance from city to city , for all pairs of cities. If there is no possible path between a pair of cities, we will use as the distance. It follows that, if or , then no solution exists. This part can easily be implemented using the Floyd-Warshall algorithm.
Let us denote by the minimum number of cities that need to be monitored so that a path is possible, where cities and do not necessarily have to be distinct. Then it is possible to move from "state" to with the following cost:
If we search the state space over all pairs using Dijkstra's algorithm, we will obtain the optimal solution in .
Comments