백준/최단경로
# 1238 파티
bright_code
2021. 4. 10. 01:05
728x90
반응형
1238번: 파티
첫째 줄에 N(1 ≤ N ≤ 1,000), M(1 ≤ M ≤ 10,000), X가 공백으로 구분되어 입력된다. 두 번째 줄부터 M+1번째 줄까지 i번째 도로의 시작점, 끝점, 그리고 이 도로를 지나는데 필요한 소요시간 Ti가 들어
www.acmicpc.net
import heapq
import sys
input = sys.stdin.readline
n, m, x = map(int,input().split())
# n 명, 도로 m 개 , x 마을에서
graph= [ [] for i in range(n+1)]
for i in range(m):
a,b,c = map(int,input().split())
graph[a].append((b,c))
def dij(start):
q = []
heapq.heappush(q, (0,start))
dist = [1e9]*(n+1)
dist[start] = 0
while q:
d, now = heapq.heappop(q)
if dist[now]<d : continue
for i in graph[now]:
cost = d + i[1]
if cost < dist[i[0]]:
dist[i[0]] = cost
heapq.heappush(q,(cost,i[0]))
return dist
result = [0]
dist_x = dij(x)
for i in range(1,n+1):
result.append( dij(i)[x] + dist_x[i] )
print ( max(result) )
728x90
반응형