SRM 470, D2, 1000-Pointer (ActivateGame)

James S. Plank

November, 2013.
Latest revision: April, 2021.

In case Topcoder's server's are down

Here is a summary of the problem: Testing yourself: do:
UNIX> g++ -o gen_input gen_input.cpp
Then enter a number as standard input to ./gen_input, and it will spit out a random problem whose constraints are 500 rather then 50. The tests.sh script assumes that you have compiled gen_input.cpp as above.

Examples

# Input                    Answer

0 "05" 69 "aB"
1 "03" 7 "21"
2 "John" 118 "Brus" "Gogo"
3 "AAA" 0 "AAA" "AAA"

Hints

This problem asks to find the maximum spanning tree on an undirected graph. Fortunately, you can implement maximum spanning tree equivalently to minimum spanning tree, except you look for large edges instead of small ones. Either Prim or Kruskal's algorithm will work. I found it easiest to define an Edge struct, and create the graph (actually, just the edges), and then use Kruskal's algorithm with the disjoint set implementation from the CS302 lecture notes.

First, here's a table of the values of letters, so you can process the examples easier:

abcdefghijklmnopqrstuvwxyz
1011121314151617181920212223242526272829303132333435

ABCDEFGHIJKLMNOPQRSTUVWXYZ
3637383940414243444546474849505152535455565758596061

Because pictures help, here's the graph for Example 2, with its maximum spanning tree.

21+7+6+8+13+5+10+14+18+8+8 = 118. It's good practice to solve this problem with both Prim and Kruskal's algorithm.