All hamiltonian cycles

Given a directed graph with arcs with positive costs, print all paths that leave the first vertex, end in the first vertex, and pass through all other vertices exactly once. Also, print the cost of each of these cycles.

Input

Input consists of several cases. Every case starts with the number of vertices nn followed by nn rows, each with nn numbers. The jj-th number of the ii-th row indicates the cost of the arc going from vertex ii to vertex jj (vertices are numbered from 0 to n1n - 1). A cost equal to zero indicates that there is no arc (the diagonal has only zeros). Assume that nn is “small” and at least 2, and that the cost of each cycle fits into an integer number.

Output

Print, in lexicographical order, all cycles of length nn that leave and end at vertex 0 without repeating vertices, and the cost of each cycle. Print a line with 20 dashes at the end of each case.

Problem information

Author: Unknown
Translator: Salvador Roura

Generation: 2026-01-25T10:16:26.963Z

© Jutge.org, 2006–2026.
https://jutge.org