UVa 117 - The Postal Worker Rings Once

117 - The Postal Worker Rings Once

 * http://acm.uva.es/p/v1/117.html

Summary
This problem reduces to a Graph, by looking at each word as a vertex, and two vertices sharing an edge if and only if they share same characters either in the first character or the last character.

We can reduce this problem to a Eulerian Path or a Eulerian Cycle problem, since each vertex will have an even number of degrees (except for at most 2 vertices).

Explanation
Even though at first glance, it seems like it might need the Chinese Postman algorithm, but since each vertex will have an even number of degrees (except for at most 2 vertices), we can use the simplier Eulerian Path/Eulerian Cycle algorithm instead. If all vertices are of even degrees, then you're done, since the solution is simply the Eulerian Cycle - the sum of the weights of all the edges. Otherwise, we will have to calculate the Eulerian Path, then you will have to find the shortest path between the two odd-degree vertices. This can be done with any of the Shortest Path algorithms.

Input
mit dartmouth linkoping tasmania york emory cornell duke kaunas hildesheim concord arkansas williams glasgow deadend one two three deadend

Output
11 114