Skip to content

Commit

Permalink
Merge pull request #195 from AlgoLeadMe/41-gjsk132
Browse files Browse the repository at this point in the history
41-gjsk132
  • Loading branch information
gjsk132 authored Sep 11, 2024
2 parents 27fed16 + ed75b1a commit 2621907
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions gjsk132/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@
| 38μ°¨μ‹œ | 2024.05.24 | BFS | [17836 κ³΅μ£Όλ‹˜μ„ ꡬ해라!](https://www.acmicpc.net/problem/17836) | [πŸ‘Έ](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/180) |
| 39μ°¨μ‹œ | 2024.05.29 | Dijkstra | [11781 퇴근 μ‹œκ°„](https://www.acmicpc.net/problem/11781) | [🚌](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/183) |
| 40μ°¨μ‹œ | 2024.06.04 | BitMasking | [[1μ°¨] 비밀지도](https://school.programmers.co.kr/learn/courses/30/lessons/17681) | [πŸ—ΊοΈ](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/190) |
| 41μ°¨μ‹œ | 2024.07.01 | TSP | [2098 μ™ΈνŒμ› 순회](https://www.acmicpc.net/problem/2098) | [✈️](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/195) |
31 changes: 31 additions & 0 deletions gjsk132/TSP/2098 μ™ΈνŒμ› 순회.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
input = open(0).readline

N = int(input())

cost = [list(map(int, input().split())) for _ in range(N)]

INF = float('inf')
dp = [[0]*((1 << N) - 1) for _ in range(N)]

def DFS(now, visited):
if visited == (1<<N)-1:
if cost[now][0]:
return cost[now][0]
else:
return INF

if dp[now][visited] != 0:
return dp[now][visited]

dp[now][visited] = INF

for i in range(1, N):
if cost[now][i] == 0:
continue
if visited & (1<<i):
continue
dp[now][visited] = min(dp[now][visited], cost[now][i]+DFS(i,visited | (1<<i)))

return dp[now][visited]

print(DFS(0,1))

0 comments on commit 2621907

Please sign in to comment.