Skip to content

Commit

Permalink
Merge pull request #69 from AlgoLeadMe/18-seongwon030
Browse files Browse the repository at this point in the history
18-seongwon030
  • Loading branch information
seongwon030 authored Jul 14, 2024
2 parents 6ecb574 + 4cf5ed2 commit 6dcfbbe
Showing 1 changed file with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import collections
import sys
from heapq import heappop, heappush

input = sys.stdin.readline

N,M,T = map(int,input().split())
graph = collections.defaultdict(list)
answer = 0
conquer_cost = 0

for _ in range(M):
a,b,c = map(int,input().split())
graph[a].append((b,c))
graph[b].append((a,c))

visited=[False for _ in range(N+1)]
heap=[(0,1)]

while heap:
cost,node = heappop(heap) # ํž™์—์„œ ์ตœ์†Œ๋น„์šฉ ๊ฐ„์„  ๊บผ๋ƒ„

if not visited[node]: # ํ•ด๋‹น ๋…ธ๋“œ ๋ฐฉ๋ฌธ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
visited[node]=True
answer += (cost + conquer_cost) # answer์— ํ˜„์žฌ ๊ฐ„์„  ๋น„์šฉ๊ณผ T์˜ ํ•ฉ์„ ๋”ํ•ด์คŒ

if node!=1: # ์ฒซ ๋ฒˆ์งธ ๋…ธ๋“œ ์•„๋‹ˆ๋ฉด, ์ •๋ณต๋น„์šฉ T ์ฆ๊ฐ€
conquer_cost += T

for next_node,next_cost in graph[node]: # ๋‹ค์Œ ๋…ธ๋“œ ํƒ์ƒ‰ํ•˜์—ฌ ํž™์— ๋„ฃ๊ธฐ
if not visited[next_node]:
heappush(heap,(next_cost,next_node))


print(answer)

0 comments on commit 6dcfbbe

Please sign in to comment.