파란색가운의 개발 블로그
[이코테] 최단경로 문제 3 [전보] 파이썬 풀이 본문
import sys
# sys.stdin.readline()
from collections import deque
sys.setrecursionlimit(1000000)
import heapq
INF = int(1e9)
N,M,C = map(int,sys.stdin.readline().split())
graph = [[] for _ in range(N+1)]
distance = [INF] * (N+1)
for i in range(M):
x,y,z = map(int,sys.stdin.readline().split())
graph[x].append((y,z))
q = []
def dijkstra(start):
distance[start] = 0
heapq.heappush(q,(0,start))
while q:
dist, now = heapq.heappop(q)
if distance[now] < dist:
continue
for i in graph[now]:
cost = dist + i[1]
if cost < distance[i[0]]:
distance[i[0]] = cost
heapq.heappush(q,(cost,i[0]))
dijkstra(C)
count = 0
max = -1
for i in distance:
if i!= INF and i!=0:
count +=1
if i > max and i < INF:
max = i
print(count,max,end='')
걸리는 총 시간 = 메시지는 동시에 송신하므로 시간중 가장 오래 걸리는 Max값을 찾아주면 되고
받는 도시는 , 자기 자신과 INF가 되지 않는, 즉 distance[i]의 원소 중에서 0과 INF가 아니라면
i번째 도시에 도달할 수 있다는 의미가 된다.
'알고리즘 문제 풀이 > 이코테' 카테고리의 다른 글
[이코테] DFS/BFS 실전문제 3 음료수 얼려 먹기 (0) | 2023.11.02 |
---|---|
[이코테 Python] 구현 실전문제 3 게임 개발 (0) | 2023.10.03 |
Chapter 4.구현 / 실전 문제 왕실의 나이트 (0) | 2023.09.29 |
[이코테] 최단경로 문제 2 [미래도시] 파이썬 풀이 (0) | 2023.07.19 |
이코테 플로이드 워셜 알고리즘 (0) | 2023.07.04 |