[๋ฐฑ์ค€ ๐Ÿฅ‡3] 17182๋ฒˆ ์šฐ์ฃผ ํƒ์‚ฌ์„  (Python/ํŒŒ์ด์ฌ)

mingssoยท2023๋…„ 3์›” 20์ผ
0

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
27/35

1๏ธโƒฃ ๋ฌธ์ œ

https://www.acmicpc.net/problem/17182



2๏ธโƒฃ ์ฝ”๋“œ

ํ”Œ๋กœ์ด๋“œ + ๋ฐฑํŠธ๋ž˜ํ‚น์œผ๋กœ ํ’€์—ˆ๋‹น

import sys

INF = sys.maxsize

n, k = map(int, input().split())
time = []
for _ in range(n):
    time.append(list(map(int, input().split())))

def floyd():
    dist = [[INF] * n for _ in range(n)]

    for i in range(n):
        for j in range(n):
            dist[i][j] = time[i][j]

    for k in range(n):
        for i in range(n):
            for j in range(n):
                if dist[i][j] > dist[i][k] + dist[k][j]:
                    dist[i][j] = dist[i][k] + dist[k][j]

    return dist

# ๋ชจ๋“  ์šฐ์ฃผ์„ ์„ ํƒ์‚ฌํ•˜๋Š” ๊ฐ ๊ฒฝ๋กœ์— ๋Œ€ํ•ด ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„ ๊ตฌํ•˜๊ธฐ 
def pick(s, visited, cnt, t):   
    if cnt == n:   # cnt๋Š” ํƒ์ƒ‰ํ•œ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜
        answer.append(t)
        return

    for i in range(n):
        if not visited[i]:
            visited[i] = True
            pick(i, visited, cnt+1, t+dist[s][i])
            visited[i] = False
        
    
dist = floyd()

visited = [False for _ in range(n)]
visited[k] = True

answer = []
pick(k, visited, 1, 0)
print(min(answer))




profile
๐Ÿฅ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ’ฐ

0๊ฐœ์˜ ๋Œ“๊ธ€