-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import heapq | ||
import sys | ||
input = sys.stdin.readline | ||
INF = int(1e9) # 무한을 의미하는 값으로 10억을 설정 | ||
|
||
# 노드의 개수, 간선의 개수, 시작 노드를 입력받기 | ||
n, m, start = map(int, input().split()) | ||
# 각 노드에 연결되어 있는 노드에 대한 정보를 담는 리스트를 만들기 | ||
graph = [[] for i in range(n + 1)] | ||
# 최단 거리 테이블을 모두 무한으로 초기화 | ||
distance = [INF] * (n + 1) | ||
|
||
# 모든 간선 정보를 입력받기 | ||
for _ in range(m): | ||
a, b, c = map(int, input().split()) | ||
# a번 노드에서 b번 노드로 가는 비용이 c라는 의미 | ||
graph[a].append((b, c)) | ||
|
||
def dijkstra(start): | ||
q = [] | ||
# 시작 노드로 가기 위한 최단 경로는 0으로 설정하여, 큐에 삽입 | ||
heapq.heappush(q, (0, start)) | ||
distance[start] = 0 | ||
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(start) | ||
|
||
# 도달할 수 있는 노드의 개수 | ||
count = 0 | ||
# 도달할 수 있는 노드 중에서 가장 멀리 있는 노드와의 최단 거리 | ||
max_distance = 0 | ||
for d in distance: | ||
# 도달할 수 있는 노드의 경우 | ||
if d!=INF: | ||
count += 1 | ||
max_distance = max(max_distance, d) | ||
print(count-1, max_distance) |