Skip to content

Commit

Permalink
Merge pull request #264 from AlgoLeadMe/85-tgyuuAn
Browse files Browse the repository at this point in the history
85-tgyuuAn
  • Loading branch information
tgyuuAn authored Jan 28, 2025
2 parents f2a2a9d + cb7c770 commit aa448d5
Show file tree
Hide file tree
Showing 2 changed files with 67 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 @@ -86,5 +86,6 @@
| 82์ฐจ์‹œ | 2024.11.22 | ํฌ์†Œ ๋ฐฐ์—ด | <a href="https://www.acmicpc.net/problem/17435">ํ•ฉ์„ฑํ•จ์ˆ˜์™€ ์ฟผ๋ฆฌ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/257
| 83์ฐจ์‹œ | 2024.12.01 | ์ˆ˜ํ•™ + ๊ตฌํ˜„ | <a href="https://www.acmicpc.net/problem/1033">์นตํ…Œ์ผ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/259
| 84์ฐจ์‹œ | 2024.12.31 | BFS | <a href="https://www.acmicpc.net/problem/4179">๋ถˆ!</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/261
| 85์ฐจ์‹œ | 2025.01.09 | ๋‹ค์ต์ŠคํŠธ๋ผ + ์ด๋ถ„ ํƒ์ƒ‰ | <a href="https://www.acmicpc.net/problem/1800">์ธํ„ฐ๋„ท ์„ค์น˜</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/264
| 86์ฐจ์‹œ | 2025.01.16 | ์ด๋ถ„ ํƒ์ƒ‰ | <a href="https://www.acmicpc.net/problem/1114">ํ†ต๋‚˜๋ฌด ์ž๋ฅด๊ธฐ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/265
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import sys
from heapq import *

def input(): return sys.stdin.readline().rstrip()

N, P, K = map(int, input().split())
graph = [[int(1e9) for _ in range(N+1)] for _ in range(N+1)]

for _ in range(P):
start, destination, cost = map(int, input().split())
graph[start][destination] = min(graph[start][destination], cost)
graph[destination][start] = min(graph[destination][start], cost)

def check(mid):
if dijkstra(mid): return True
return False

def dijkstra(_max):
visited = set()
DP = [[int(1e9) for _ in range(K+1)] for _ in range(N+1)]
DP[1][K] = 0

# ๊ฐ„์„  ์ฝ”์ŠคํŠธ, ๋‚จ์€ ๊ธฐํšŒ, ํ˜„์žฌ ๋…ธ๋“œ
heap = [(0, K, 1)]
while heap:
now_cost, now_remain, now_node = heappop(heap)

if (now_node, now_remain) in visited: continue
visited.add((now_node, now_remain))

if DP[now_node][now_remain] < now_cost: continue
DP[now_node][now_remain] = now_cost

if now_node == N:
return True

for idx, cost in enumerate(graph[now_node]):
if idx == 0: continue
if cost == int(1e9): continue

if cost <= _max:
if (idx, now_remain) in visited: continue
if DP[idx][now_remain] > cost: heappush(heap, (cost, now_remain, idx))

elif cost > _max and now_remain >= 1:
if (idx, now_remain-1) in visited: continue
if DP[idx][now_remain-1] > cost: heappush(heap, (cost, now_remain-1, idx))

for idx in range(1, K+1):
if DP[N][idx] != int(1e9): return True

return False

answer = int(1e9)
left = -1
right = 1_000_001
while left+1<right:
mid = (left + right) // 2

if check(mid):
right = mid
answer = mid
else: left = mid

if answer != int(1e9): print(answer)
else: print(-1)

0 comments on commit aa448d5

Please sign in to comment.