-
Notifications
You must be signed in to change notification settings - Fork 0
/
1504 특정한 최단 경로.py
53 lines (36 loc) · 949 Bytes
/
1504 특정한 최단 경로.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import sys
import heapq
input = sys.stdin.readline
n,m= map(int,input().split())
front = [[] for _ in range(n+1)]
INF = 10e8
answer =[]
for _ in range(m):
a,b,c = map(int,input().split())
front[a].append((c,b))
front[b].append((c,a))
viaStart,viaEnd = map(int,input().split())
def daik(start):
visited = [False] * (n+1)
arr=[INF] * (n+1)
q = []
arr[start] = 0
heapq.heappush(q,(0,start))
while q:
price,startnode = heapq.heappop(q)
for r,c in front[startnode]:
if arr[c] > arr[startnode] + r:
arr[c] = arr[startnode] + r
heapq.heappush(q,(arr[c],c))
return arr
answer.append(daik(1)[viaStart] + daik(viaStart)[viaEnd] + daik(viaEnd)[n])
answer.append(daik(1)[viaEnd] + daik(viaEnd)[viaStart] + daik(viaStart)[n])
if min(answer) >=INF:
print(-1)
else:
print(min(answer))
# 경우의 수
'''
1 a b n
1 b a n
'''