Editorial
Rotating a Hamiltonian cycle does not change it, so fix vertex as the start.
Let be the minimum weight of a path that starts at vertex , visits every vertex of exactly once, and ends at . Initially, . If an edge from to an unvisited vertex exists, update with .
After visiting all vertices, add the weight of the edge returning to vertex and choose the minimum. If no such value exists, print . Store the predecessor of each improved state to reconstruct the optimal cycle. Edge weight is valid, so test for a missing edge using . A cycle can cost up to , requiring 64-bit integers.
The time complexity is and the space complexity is . For subtask , one can also fix the starting vertex and check every permutation of the remaining vertices.
Solution written by GPT6