Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import sys
import heapq

input = sys.stdin.readline
INF = float('inf')

N, E = map(int, input().split())
graph = [[] for _ in range(N + 1)]
for _ in range(E):
a, b, c = map(int, input().split())
graph[a].append((b, c))
graph[b].append((a, c))
v1, v2 = map(int, input().split())



def dijkstra(start, end):
dist = [INF] * (N + 1)
dist[start] = 0
hq = [(0, start)]
while hq:
len, node = heapq.heappop(hq)
if len > dist[node]:
continue
for next_node, val in graph[node]:
if dist[next_node] > dist[node] + val:
dist[next_node] = dist[node] + val
heapq.heappush(hq, (dist[next_node], next_node))
return dist[end]


path1 = dijkstra(1, v1) + dijkstra(v1, v2) + dijkstra(v2, N)
path2 = dijkstra(1, v2) + dijkstra(v2, v1) + dijkstra(v1, N)

print(-1) if path1 >= INF and path2 >= INF else print(min(path1, path2))
41 changes: 41 additions & 0 deletions YoonYn9915/dijkstra/2025-06-28-[백준]-#1753-최단 경로.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import heapq
import sys

input = sys.stdin.readline

V, E = map(int, input().split())
start = int(input())
graph = [[] for _ in range(V + 1)]

for _ in range(E):
u, v, w = map(int, input().split())

graph[u].append((v, w))


def dijkstra(start):
distances = [float("inf")] * (V + 1)
distances[start] = 0
q = []
heapq.heappush(q, (distances[start], start))

while q:
cnt_distance, node = heapq.heappop(q)

if distances[node] < cnt_distance:
continue

for adjacency_node, distance in graph[node]:
cal_distance = distances[node] + distance

if cal_distance < distances[adjacency_node]:
distances[adjacency_node] = cal_distance
heapq.heappush(q, (cal_distance, adjacency_node))

return distances


result = dijkstra(start)

for i in range(1, len(result)):
print("INF" if result[i] == float("inf") else result[i])
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
N = int(input())

arr = []

for _ in range(N):
q, p = list(map(int, input().split()))
arr.append([q, p])

arr.sort(reverse=True)

count = 0
for i in range(5, len(arr)):
if arr[4][0] == arr[i][0]:
count += 1
else:
break

print(count)