Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

41-gjsk132 #195

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))