-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1238.py
48 lines (35 loc) · 875 Bytes
/
1238.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
# 파티
import sys
import heapq
from collections import defaultdict
input = sys.stdin.readline
N, M, X = map(int, input().split())
roads = defaultdict(list)
for _ in range(M):
S, E, T = map(int, input().split())
roads[S].append([T, E])
INF = 10**6
def move(Start):
des = [[0, Start]]
min_time = [INF for _ in range(N + 1)]
while des:
T, E = heapq.heappop(des)
if min_time[E] <= T:
continue
min_time[E] = T
for nextT, nextE in roads[E]:
heapq.heappush(des, [nextT + T, nextE])
if Start == X:
return min_time
else:
return min_time[X]
time = [0 for _ in range(N + 1)]
for i in range(1, N + 1):
if i == X:
back = move(i)
for j in range(len(back)):
time[j] += back[j]
else:
time[i] += move(i)
time[0] = 0
print(max(time))