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:
- You are given a rectangular grid of nodes as a vector of strings.
- The characters of the strings are digits, upper-case and lower-case values.
- The value of a digit is the digit's value.
- The values of 'a' through 'z' are 10 through 35.
- The values of 'A' through 'Z' are 36 through 61.
- You are playing a game where you are going to "activate" nodes.
- The node in cell (0,0) starts activated. The rest are not activated.
- You will proceed in turns until every node is activated.
- In each turn, you activate a non-activated node which is either horizontally or vertically adjacent to an activated node. When you activate a node, you "score" the difference between the non-activated node and the activated node to which it is connected.
- Return the maximum score of activating all nodes.
- The Topcoder constraints are vectors of size 50 with at most 50 characters. You can
bump this up quite a bit, since the algorithm is O(E log V), and E = O(V),
so ./gen_input uses 500 as the constraint instead of 50.
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:
a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z |
10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 |
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 |
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.