Editorial for WC '17 Finals S2 - Cowmmunication Network
Submitting an official solution before solving the problem yourself is a bannable offence.
This problem can be represented as a graph, with nodes (one for each combat unit) and undirected, weighted edges (one for each potential communication channel). We're then asked to find a subset of the edges with maximum total weight which result in the entire graph being connected. This sounds very similar to finding the minimum spanning tree of the graph – in this case, more of its "maximum spanning tree", which is essentially the same thing. Note that any algorithm for finding a graph's minimum spanning tree could instead find the maximum spanning tree by inverting some comparisons.
So, the only real difference is that we're allowed to use as many edges as we want, rather than exactly edges. Note that this difference goes away if all edges have negative weights, as we'd want to use as few of them as possible (in other words, of them).
The fact that some edges can have positive weights ends up not changing the solution too significantly. All such edges should clearly be used, even if they are not necessary for the graph's maximum spanning tree. Therefore, we can essentially find the maximum spanning tree as normal, and then additionally use all remaining edges with positive weights.
There are two standard algorithms for finding maximum spanning trees: Kruskal's and Prim's. Both can be slightly modified to solve this problem. Kruskal's works out very simply – when processing each edge (in non-increasing order of weight), it should still be used if it would connect two nodes which aren't yet connected, but it should also be used if its weight is positive. Prim's takes a bit more work, but one way to modify it is to keep track of exactly which edges get used in the maximum spanning tree, and then add on all unused positive-weight edges at the end. Both algorithms can be implemented in time.
Comments