29 lines
690 B
Python
29 lines
690 B
Python
import sys
|
|
input = sys.stdin.readline
|
|
Inf = sys.maxsize
|
|
|
|
N = int(input())
|
|
W = [list(map(int, input().split())) for _ in range(N)]
|
|
|
|
dp = [[None] * (2**N) for _ in range(N)]
|
|
|
|
def trevel(now, visited) :
|
|
if visited == ((1 << N) - 1) :
|
|
return w if (w := W[now][0]) != 0 else Inf
|
|
|
|
if dp[now][visited] is not None :
|
|
return dp[now][visited]
|
|
|
|
min_cost = Inf
|
|
for nxt in range(N) :
|
|
if W[now][nxt] == 0 or (visited & (1 << nxt)) != 0 :
|
|
continue
|
|
|
|
cost = W[now][nxt] + trevel(nxt, visited | (1 << nxt))
|
|
min_cost = min(min_cost, cost)
|
|
|
|
dp[now][visited] = min_cost
|
|
|
|
return dp[now][visited]
|
|
|
|
print(trevel(0, 1)) |