Official

C - Travel Editorial by evima


Conduct an exhaustive search on the order of visiting Town \(2\) to \(N\). There are \((N-1)!\) such ways, and for each way, we need \(O(N)\) time to calculate the total time of travel, so the entire problem can be solved in a total of \(O(N!)\) time. Note that you should start to travel and end traveling at Town \(1\).

The exhaustive search on the order of visiting Town \(2\) to \(N\) can be achieved by next_permutation in C++ or itertools.permutations in Python. Since C and Java do not have a standard function to iterate over all the permutations, so you have to implement it properly by yourself.

Sample Code (C++)

Sample Code (Python)

Sample Code (C)

posted:
last update: