Skip to content

Commit cd7d516

Browse files
committed
[BOJ] 특정한 최단 경로 / 골드 4 / 70분 실패 힌트사용
https://www.acmicpc.net/problem/1504
1 parent 3263a8e commit cd7d516

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import sys
2+
import heapq
3+
4+
input = sys.stdin.readline
5+
INF = float('inf')
6+
7+
N, E = map(int, input().split())
8+
graph = [[] for _ in range(N + 1)]
9+
for _ in range(E):
10+
a, b, c = map(int, input().split())
11+
graph[a].append((b, c))
12+
graph[b].append((a, c))
13+
v1, v2 = map(int, input().split())
14+
15+
16+
17+
def dijkstra(start, end):
18+
dist = [INF] * (N + 1)
19+
dist[start] = 0
20+
hq = [(0, start)]
21+
while hq:
22+
len, node = heapq.heappop(hq)
23+
if len > dist[node]:
24+
continue
25+
for next_node, val in graph[node]:
26+
if dist[next_node] > dist[node] + val:
27+
dist[next_node] = dist[node] + val
28+
heapq.heappush(hq, (dist[next_node], next_node))
29+
return dist[end]
30+
31+
32+
path1 = dijkstra(1, v1) + dijkstra(v1, v2) + dijkstra(v2, N)
33+
path2 = dijkstra(1, v2) + dijkstra(v2, v1) + dijkstra(v1, N)
34+
35+
print(-1) if path1 >= INF and path2 >= INF else print(min(path1, path2))

0 commit comments

Comments
 (0)