The key to figuring out this problem was to recognize that you needed to resolve cycles in the data –
if you follow an item to the item that’s in its spot, and then the one that’s in the next one’s spot, until you
reach the original item again, you’ll have a cycle of elements that should be in each others’ spots. If an item
is already in its spot, it forms a cycle of one element. The minimum cost of resolving a cycle is (sum of the
weights in the cycle)+(number of elements in the cycle-2)*(minimum weight in cycle). This cost is zero for
one-element cycles and the sum of the elements for two-element cycles. For larger cycles, it is the cost of
swapping the smallest element in the cycle with each element until all of them are in the right spot.

The tricky part of this problem is figuring out that simply resolving all the cycles doesn’t always give you the
best answer. Sometimes the optimal answer comes by swapping the smallest element of the whole set into the cycle
and then resolving the new cycle. The cost of swapping in the smallest element is the sum of that weight and the
smallest weight in the cycle. The cost of resolving the new cycle is (sum of the weights in the cycle + minimum
of all weights) + (number of elements in the cycle + 1) * (minimum of all weights). Sometimes this creates a
smaller final value and sometimes it makes it bigger. The key to this problem was figuring out which cycles
should merge with the smallest element and which should just be resolved.

[highlight]minimum of (sum of the weights in the cycle)+(number of elements in the cycle-2)*(minimum weight in cycle)
2*(minimum_of_all_weights + minimum_weight_cycle) + (sum_of_weights_in_cycle – minimum_weight_cycle)
+(number of elements in the cycle -1)* minimum_of_all_weights[/highlight]



Leave a Reply

Your email address will not be published. Required fields are marked *