From 397c018b1cf43494b246870aa9fe1dc3770d26ee Mon Sep 17 00:00:00 2001 From: heeheejj Date: Tue, 2 Jan 2024 14:34:13 +0900 Subject: [PATCH] =?UTF-8?q?Week31=20PRG=2049189=20=EA=B0=80=EC=9E=A5=20?= =?UTF-8?q?=EB=A8=BC=20=EB=85=B8=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ..._\353\250\274_\353\205\270\353\223\234.py" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 "heeheej/week31/PRG_49189_\352\260\200\354\236\245_\353\250\274_\353\205\270\353\223\234.py" diff --git "a/heeheej/week31/PRG_49189_\352\260\200\354\236\245_\353\250\274_\353\205\270\353\223\234.py" "b/heeheej/week31/PRG_49189_\352\260\200\354\236\245_\353\250\274_\353\205\270\353\223\234.py" new file mode 100644 index 0000000..1676caf --- /dev/null +++ "b/heeheej/week31/PRG_49189_\352\260\200\354\236\245_\353\250\274_\353\205\270\353\223\234.py" @@ -0,0 +1,42 @@ +# 가장 먼 노드 +# 프로그래머스 코딩테스트 고득점 kit > 그래프 +# 다익스트라 이용해서 1번 노드로부터 다른 모든 노드까지의 최단거리 구하고, INF를 제외한 최대값 찾아서 그 개수를 구한다. +# 1287 (+7) + +import heapq + +def dijkstra(start, distance, graph): + + q = [] + heapq.heappush(q, (0, start)) + distance[start] = 0 + M = -1 # 1번 노드에서 가장 멀리 떨어진 노드까지의 거리 + + while q: + dis, now = heapq.heappop(q) + if distance[now] < dis: + continue + + for nxt in graph[now]: + cost = dis + 1 + if cost < distance[nxt]: + heapq.heappush(q, (cost, nxt)) + distance[nxt] = cost + M = max(M, cost) + # print(q, distance) + return M + +def solution(n, edge): + answer = 0 + + INF = int(1e9) + distance = [INF]*(n+1) + graph = [[] for _ in range(n+1)] + + for a, b in edge: + graph[a].append(b) + graph[b].append(a) + + M = dijkstra(1, distance, graph) + answer = distance.count(M) + return answer \ No newline at end of file