Hints for TCO 2008, Q3 500-Pointer (CableDonation)

James S. Plank

Sun Nov 24 18:53:42 EST 2013
Problem Statement.
It should be clear that this is a minimum spanning tree problem. The only subtleties are that you have to convert those letters to numbers, and then you have to discard unnecessary cables. Once you do that, you have to make sure that you create the proper undirected graph for Prim's algorithm.

Interestingly, if you use Kruskal's algorithm instead, you don't even have to discard cables or worry about direction. When you come upon a duplicate cable, or even a cable that goes from a node to itself, the algorithm won't consider it because the cable does not span disjoint sets. Think about that.

Let's see how it works on example 4, which is the most complex. Here's the graph:

You can see that if you use Kruskal's algorithm, it will process the edges in increasing order of size. It will process edge (1,0) first and (1,3) second. At that point, there will be two connected components: {0, 1, 3} and {2}. There are two edges of weight 5, so suppose it processes edge (3,1). It will see that this edge does not connect different sets, so it will ignore the edge. The next edge, (2, 0) does connect different sets. At that point, there is just one set, so we're done. The total cable donation is the total of all of the edges, which is 146, minus the edges in the minimum spanning tree, which is 12. The answer is 134.