Hints for SRM 596, D2, 500-Pointer (ColorfulRoad)

James S. Plank

Sun Nov 3 11:08:44 EST 2013
Problem Statement.
I'm sure that there are multiple ways to do this. Given how small the constraints are, and the fact that the graph is acyclic, I'm guessing that dynamic programming or topological sort will work. However, it's really easy to turn the string into a weighted graph, and then use Dijkstra's algorithm to find the shortest path from the first character to the last. It took me nine minutes to code it from scratch, and again, it's great practice.

Below, I'll draw graphs for the first two examples. From that, you should be able to see how you create a graph from a string, and then it is up to you to code up Dijkstra's algorithm.

Example 0:

Example 1:

Example 3: