problems / 100

The Drone's Grand Tour

medium bitmask dp graphs shortest-paths 1000 ms · 256 MB

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] = 0 for every i.
  • for i ≠ j, t[i][j] is either -1, meaning there is no direct flight from i to j, or an integer with 1 ≤ t[i][j] ≤ 10000, the flight time of the direct route from i to j.

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.

Example 1

Input

3
0 3 4
5 0 2
2 6 0

Output

7

Example 2

Input

4
0 1 1000 1000
1 0 1 1000
1000 1 0 1
1000 1000 1 0

Output

6

Hints · reveal one at a time if you're stuck

Hint 1

Flight times don't satisfy the triangle inequality, but revisiting stations is free. What do you get if you replace each direct flight time by the cheapest route between the same two stations?

Hint 2

After replacing each time by its shortest-path distance, the problem becomes finding the cheapest tour through all stations and back to station 0 where each station is visited once. With at most 16 stations, this is a small travelling-salesman problem.

Hint 3

Solve it with a DP over subsets of stations: the state is the set visited so far, which always includes station 0, together with the station you are currently at. Extend the path one station at a time, and finish by adding the cost of returning to station 0.

report an issue Prefer your own editor? sooks prep 100 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.