Skip to content

Commit

Permalink
71-tgyuuAn
Browse files Browse the repository at this point in the history
  • Loading branch information
tgyuuAn committed Aug 20, 2024
1 parent ae7f20b commit 64cefd0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@
| 68์ฐจ์‹œ | 2024.08.06 | ๊ทธ๋ฆฌ๋”” | <a href="https://www.acmicpc.net/problem/24337">๊ฐ€ํฌ์™€ ํƒ‘</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/226
| 69์ฐจ์‹œ | 2024.08.10 | ๋ˆ„์ ํ•ฉ, ์ˆ˜ํ•™ | <a href="https://www.acmicpc.net/problem/9527">1์˜ ๊ฐœ์ˆ˜ ์„ธ๊ธฐ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/228
| 70์ฐจ์‹œ | 2024.08.16 | ์Šคํƒ | <a href="https://www.acmicpc.net/problem/22866">ํƒ‘ ๋ณด๊ธฐ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/232
| 71์ฐจ์‹œ | 2024.08.20 | ๋‹ค์ต์ŠคํŠธ๋ผ | <a href="https://www.acmicpc.net/problem/24042">๋‹ค์ต์ŠคํŠธ๋ผ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/235
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from collections import defaultdict
from heapq import *
import sys

def input(): return sys.stdin.readline().rstrip()
MAX = 100_000 * 700_000 + 1
N, M = map(int, input().split())

graph = defaultdict(lambda : defaultdict(list))

for idx in range(1,M+1):
start, destination = map(int, input().split())
graph[start][destination].append(idx)
graph[destination][start].append(idx)

costs = [MAX for _ in range(N+1)]
visited = set()
heap = [(0, 1)]
while heap:
now_cost, now_idx = heappop(heap)

if now_idx in visited: continue
visited.add(now_idx)

if costs[now_idx] <= now_cost: continue
costs[now_idx] = now_cost

if now_idx == N: break

for neighbor in graph[now_idx]:
if neighbor in visited: continue

for neighbor_idx in graph[now_idx][neighbor]:

real_idx = now_cost % M
if real_idx < neighbor_idx:
heappush(heap, (neighbor_idx - real_idx + now_cost, neighbor))
else:
heappush(heap, (now_cost + M - real_idx + neighbor_idx, neighbor))
print(costs[-1])

0 comments on commit 64cefd0

Please sign in to comment.