The Drone's Grand Tour
A survey drone is based at station 0. Before it can be recalled, it must land at
every other station at least once, and then fly back home to station 0.
There are n stations, numbered 0 through n-1, where station 0 is the home
base. For some ordered pairs of stations (i, j) there is a direct flight route
from i to j with a known flight time; the drone can only move by taking these
direct routes, one at a time, landing wherever it arrives. A route from i to j
does not necessarily mean there is also a route from j to i — crosswinds make
the two directions independent.
Flight times also do not obey the triangle inequality: because of local
weather and terrain, flying directly from i to j can take longer than flying
from i to some k and then from k to j. The drone is free to pass through
or land at a station more than once — extra landings cost nothing beyond the
flight time itself and don't hurt it in any way, as long as every station has
been landed on at least once by the time it returns home.
Given the direct flight time between every ordered pair of stations, find the
minimum total flight time for a route that starts at station 0, lands at every
station at least once (in any order, and possibly landing at some stations more
than once), and finishes back at station 0.
Input
The first line contains a single integer n (1 ≤ n ≤ 16), the number of
stations.
Each of the next n lines contains n integers. The j-th integer on the
i-th of these lines is t[i][j]:
t[i][i] = 0for everyi.- for
i ≠ j,t[i][j]is either-1, meaning there is no direct flight fromitoj, or an integer with1 ≤ t[i][j] ≤ 10000, the flight time of the direct route fromitoj.
It is guaranteed that, using only the direct routes described above (possibly through several intermediate stations), the drone can always fly from any station to any other station — so a valid tour that lands at every station and returns home always exists.
Output
Print a single integer: the minimum total flight time of a route that starts
at 0, lands at every station at least once, and returns to 0.
Notes
In the first example there are 3 stations. Flying 0 → 1 → 2 → 0 costs
3 + 2 + 2 = 7, which is cheaper than the only other order, 0 → 2 → 1 → 0
(costing 4 + 6 + 5 = 15), so 7 is the answer.
In the second example, stations 1 and 2 only have cheap direct flights to
their neighbours on a chain 0 - 1 - 2 - 3; every other direct route costs
1000. Flying all the way down the chain and back, 0 → 1 → 2 → 3 → 2 → 1 → 0,
costs only 1 + 1 + 1 + 1 + 1 + 1 = 6. Landing at 1 and 2 twice is free of
penalty, so this beats any route that insists on landing at each station exactly
once.