This page is still under construction.

Parts of this page are still being built. What you see may change.

Traveling Salesman Tour 2

Time limit2sMemory limit256 MB

Summary
Find the cheapest tour that starts at one city, visits each of N cities exactly once, and returns to the start using the given directed costs.
Level

Medium5 of 10

Topics
Dynamic programming, Bit manipulation, Graph
Solved
No attempts yet

Problem

The traveling salesman problem, TSP for short, is one of the most important problems in computer science. Several variants of it exist. This task uses the most common form.

Cities are numbered 1 through NN, and roads run between cities. Some pairs of cities have no road. A salesman wants to plan a tour that starts at one city, passes through all NN cities, and returns to the city he started from. He may not go back to a city he has already visited. Returning to the starting city at the very end is the only exception. Many such tours exist, and he wants the plan with the smallest total cost.

The cost of moving between cities is given as a matrix WW. W[i][j]W[i][j] is the cost of going from city ii to city jj. The costs are not symmetric, so W[i][j]W[i][j] may differ from W[j][i]W[j][i]. Every travel cost is a positive integer, and W[i][i]W[i][i] is always 0. There are cases where city jj cannot be reached from city ii, and such a case is written as W[i][j]=0W[i][j] = 0.

Given NN and the cost matrix, write a program that finds the minimum cost of the salesman's tour.

Input

The first line contains the number of cities NN. (2≤N≤102 \le N \le 10) Each of the next NN lines contains NN entries of the cost matrix. Each entry is a positive integer no greater than 1,000,000, and 0 is given when the move is impossible. W[i][j]W[i][j] is the cost of going from city ii to city jj.

Every input allows at least one tour.

Output

Print the minimum cost of the salesman's tour on the first line.

Examples5

  1. Example 1

    Input
    4
    0 10 15 20
    5 0 9 10
    6 13 0 12
    8 8 9 0
    
    Expected output
    35
    
  2. Example 2

    Input
    2
    0 1
    1 0
    
    Expected output
    2
    
  3. Example 3

    Input
    2
    0 1000000
    1000000 0
    
    Expected output
    2000000
    
  4. Example 4

    Input
    3
    0 1 100
    100 0 1
    1 100 0
    
    Expected output
    3
    
  5. Example 5

    Input
    3
    0 4 0
    0 0 5
    6 0 0
    
    Expected output
    15