Skip to content

Commit 4a4a326

Browse files
committed
골목 대장 호석 풀이
1 parent 3761356 commit 4a4a326

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# 그래프 이론, 이분 탐색, 최단 경로, 데이크스트라, 매개 변수 탐색
2+
# https://www.acmicpc.net/problem/20183
3+
4+
"""
5+
5 5 1 3 10
6+
1 2 5
7+
2 3 5
8+
1 4 1
9+
4 5 6
10+
5 3 1
11+
"""
12+
13+
input = __import__("sys").stdin.readline
14+
15+
N,M,A,B,C = map(int, input().split())
16+
graph = [[] for _ in range(N+1)]
17+
costs = []
18+
for _ in range(M):
19+
a,b,c = map(int, input().split())
20+
graph[a].append((b,c))
21+
graph[b].append((a,c))
22+
costs.append(c)
23+
24+
costs.sort()
25+
26+
INF = int(1e15)
27+
28+
def dijkstra(limit_cost):
29+
import heapq
30+
31+
heap = [(0,A)] # 비용, 노드
32+
dist = [INF]*(N+1) # 비용
33+
dist[A] = 0
34+
35+
while heap:
36+
cost, node = heapq.heappop(heap)
37+
38+
if dist[node] < cost:
39+
continue
40+
41+
for nxt_node, nxt_cost in graph[node]:
42+
sum_cost = dist[node] + nxt_cost
43+
44+
if limit_cost < nxt_cost:
45+
continue
46+
47+
if sum_cost > C or dist[nxt_node] <= sum_cost:
48+
continue
49+
50+
dist[nxt_node] = sum_cost
51+
52+
heapq.heappush(heap, (sum_cost, nxt_node))
53+
54+
return dist[B]
55+
56+
l = 0
57+
r = M-1
58+
answer = INF
59+
60+
while l <= r:
61+
mid = (l+r)//2
62+
cost = dijkstra(costs[mid])
63+
if cost == INF:
64+
l = mid+1
65+
else:
66+
r = mid-1
67+
answer = min(costs[mid], answer)
68+
69+
print(-1 if answer == INF else answer)

0 commit comments

Comments
 (0)