Skip to content

Commit 2ed58ee

Browse files
committed
0711
1 parent b64a7a3 commit 2ed58ee

File tree

3 files changed

+87
-51
lines changed

3 files changed

+87
-51
lines changed

.idea/workspace.xml

Lines changed: 50 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

solve.ac/class 4/1865_웜홀/1865_웜홀.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ def BellmanFord(start):
1515
# 다음 노드로 이동하는 거리가 최단거리로 갱신가능한 경우
1616
if distance[next] > distance[i] + cost:
1717
distance[next] = cost + distance[i]
18+
# (정점의 개수 - 1)번까지 계속 업데이트가 발생했을 경우
19+
# (정점의 개수)번도 업데이트 발생하면 음수 사이클이 일어난 것을 의미함.
20+
#V - 1개의 간선보다 더 많은 간선을 통해 최단경로를 구할 수 있다면 음의 사이클이 존재한다고 판단
1821
if k == N:
1922
return True
2023
return False
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import heapq, sys
2+
3+
input = sys.stdin.readline
4+
INF = int(1e9)
5+
6+
7+
def dijkstra(start):
8+
distance = [INF] * (N + 1)
9+
distance[start] = 0
10+
heap = []
11+
heapq.heappush(heap, [0, start])
12+
while heap:
13+
cost, now = heapq.heappop(heap)
14+
#등록돼 있는 distance[now]가 cost(지금 거리) 보다 더 작은경우
15+
if cost > distance[now]:
16+
continue
17+
for next, nextCost in graph[now]:
18+
nextTotal = nextCost + cost
19+
if distance[next] > nextTotal:
20+
distance[next] = nextTotal
21+
heapq.heappush(heap, [nextTotal, next])
22+
return distance
23+
24+
25+
N = int(input())
26+
M = int(input())
27+
graph = [[] for _ in range(N + 1)]
28+
for _ in range(M):
29+
S, E, C = map(int, input().split())
30+
graph[S].append((E, C))
31+
32+
startPoint, endPoint = map(int, input().split())
33+
result = dijkstra(startPoint)
34+
print(result[endPoint])

0 commit comments

Comments
 (0)