소스코드

import sys
input = sys.stdin.readline
N = int(input())
graph = [list(map(int, input().strip().split())) for _ in range(N)]

visited = [False] * N
min_cost = float('inf')
def solve(start, current, depth, cost):
	global visited
	global min_cost
	
	# 이미 비용이 더 비싸면 더이상 재귀 안 하기
	if cost >= min_cost:
		return
	
	if depth == N:
		# 다시 원점으로 복귀하는 시점
		if graph[current][start] != 0:
			min_cost = min(min_cost, cost + graph[current][start])
		return 
	
	for next_city in range(N):
		if not visited[next_city] and graph[current][next_city]:
			visited[next_city] = True
			solve(start, next_city, depth+1, cost + graph[current][next_city])
			visited[next_city] = False
			
for i in range(N):
	visited[i] = True
	solve(i,i,1,0)
	visited[i] = False
	
sys.stdout.write(str(min_cost) + '\\n')

풀이