-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10971.py
38 lines (28 loc) · 970 Bytes
/
10971.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sys
input = sys.stdin.readline
MAX_COST = 10_000_000
HOMETOWN = 0
def solution():
global min_cost
min_cost = MAX_COST
is_visited[HOMETOWN] = True
backtrack(HOMETOWN, 1, 0)
return min_cost
def backtrack(current_node: int, num_visit: int, current_cost: int):
global min_cost
if current_cost > min_cost:
return
if num_visit == n and dist[current_node][HOMETOWN]:
min_cost = min(min_cost, current_cost + dist[current_node][HOMETOWN])
return
for next_node in range(n):
if (not is_visited[next_node]) and dist[current_node][next_node]:
is_visited[next_node] = True
backtrack(next_node, num_visit + 1, current_cost + dist[current_node][next_node])
is_visited[next_node] = False
if __name__ == '__main__':
n = int(input())
dist = [list(map(int, input().split())) for col in range(n)]
is_visited = [False] * n
sol = solution()
print(sol)