Editorial for COCI '09 Contest 7 #4 Svemir


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.

First, you need to know at least one way of constructing minimum spanning trees, e.g. Kruskal's algorithm.

We claim that tunnels need to be constructed only between planets that are immediate neighbors on one of the axis. This can be easily proven. Suppose there are two planets (A and B) and that tunnel cost between A and B is |x_A-x_B| \le \min\{|y_A-y_B|, |z_A-z_B|\}. Now suppose that A and B are not immediate neighbors on the X axis. In other words, there is some planet C such that x_A \le x_C \le x_B. It is easy to see that instead of constructing one tunnel from A to B we can construct two tunnels: (A,C) and (C,B) that cost less than or equal to tunnel (A,b) since dist(A,C) + dist(C,B) \le |x_A-x_C| + |x_C-x_B| = |x_A-x_B|.

We are nearly done now. We simply construct a graph of all planets with links between immediate neighbors. Using Kruskal's algorithm, we obtain a minimal cost fully connected network.


Comments

There are no comments at the moment.