forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYetAnotherHamiltonianPath.html
More file actions
13 lines (13 loc) · 4.96 KB
/
Copy pathYetAnotherHamiltonianPath.html
File metadata and controls
13 lines (13 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>    </td><td>A Hamiltonian path in an undirected graph with N vertices is a sequence of vertices A<sub>1</sub>, A<sub>2</sub>, ..., A<sub>N</sub> such that all A<sub>i</sub> are pairwise distinct and for each i (1 < i &le N), there is an edge between vertices A<sub>i-1</sub> and A<sub>i</sub>. A path starts at vertex A<sub>1</sub> and visits each vertex of the sequence in order, ending at vertex A<sub>N</sub>. The cost of a path is the sum of the weights of the edges connecting the path's consecutive vertices.
<br></br><br></br>
You're given a graph where the i-th (0-based) vertex is labeled with string <b>label</b>[i]. There is an edge between each pair of vertices. The cost of the edge between vertices i and j is equal to <i>length^2 (<b>label</b>[i]) + length^2 (<b>label</b>[j]) - length^2 (LCP (<b>label</b>[i], <b>label</b>[j]) )</i>, where "^2" denotes squaring operation, <i>length(X)</i> is the length of string X and <i>LCP(X,Y)</i> is the longest common prefix of strings X and Y.
<br></br><br></br>
Return the minimum possible cost of a Hamiltonian path which starts at vertex 0 and ends at vertex 1.</td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>    </td><td><table><tr><td>Class:</td><td>YetAnotherHamiltonianPath</td></tr><tr><td>Method:</td><td>leastCost</td></tr><tr><td>Parameters:</td><td>vector <string></td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int leastCost(vector <string> label)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td>    </td></tr><tr><td></td></tr><tr><td colspan="2"><h3>Notes</h3></td></tr><tr><td align="center" valign="top">-</td><td>A prefix of string S is a string that can be obtained by removing zero or more contiguous characters from the end of S.</td></tr><tr><td align="center" valign="top">-</td><td>The longest common prefix of two strings A and B is the longest possible string which is a prefix of both A and B.</td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>label</b> will contain between 2 and 50 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>label</b> will be between 1 and 50 characters long, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>label</b> will consist of lowercase letters ('a'-'z') only.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{"home", "school", "pub"} </pre></td></tr></table></td></tr><tr><td><pre>Returns: 70</pre></td></tr><tr><td><table><tr><td colspan="2">The only possible Hamiltonian path from vertex 0 to vertex 1 is 0->2->1. Vertex 0 is labeled "home", and vertex 1 is labeled "pub", so since these two strings have no common prefix, the cost of the edge 0->1 is 4^2+3^2=25. The cost of the edge 2->1 is 45, so the total cost of the path is 70.
</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{"school", "home", "pub", "stadium"}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 167</pre></td></tr><tr><td><table><tr><td colspan="2">Of the two possible Hamiltonian paths, the cost of the one that visits "stadium" right after "school" is 1 less than the other one's.
</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{"abcd","aecgh","abef","aecd"}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 91</pre></td></tr><tr><td><table><tr><td colspan="2">The cost matrix of this graph is:<br></br>
-- 40 28 31<br></br>
40 -- 40 32<br></br>
28 40 -- 31<br></br>
31 32 31 --<br></br>
<br></br>
The optimal path is "abcd"->"abef"->"aecd"->"aecgh".</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{"canada", "cyprus", "croatia", "colombia", "chile", "china", "cameroon"}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 509</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>