Skip to content

Commit e86fd15

Browse files
committed
0726
1 parent 56b1d40 commit e86fd15

File tree

3 files changed

+133
-51
lines changed

3 files changed

+133
-51
lines changed

.idea/workspace.xml

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

solve.ac/class 4/1504_특정한 최단 경로/1504_특정한 최단 경로.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
INF = int(1e9)
1010

1111

12-
def dikjestra(start):
12+
def dikjstra(start):
1313
distance = [INF] * (N + 1)
1414
distance[start] = 0
1515
heap = []
@@ -33,7 +33,7 @@ def dikjestra(start):
3333
grape[e].append((s, v))
3434
u, v = map(int, input().split())
3535

36-
root = dikjestra(1)[u] + dikjestra(u)[v] + dikjestra(v)[N]
37-
backroot = dikjestra(1)[v] + dikjestra(v)[u] + dikjestra(u)[N]
36+
root = dikjstra(1)[u] + dikjstra(u)[v] + dikjstra(v)[N]
37+
backroot = dikjstra(1)[v] + dikjstra(v)[u] + dikjstra(u)[N]
3838
result = min(root, backroot)
3939
print(-1 if INF <= result else result)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import heapq
2+
3+
INF = int(1e9)
4+
5+
6+
def dikjstra(start):
7+
distance = [INF] * (V + 1)
8+
distance[start] = 0
9+
heap = []
10+
heapq.heappush(heap, [0, start])
11+
while heap:
12+
cost, now = heapq.heappop(heap)
13+
for next, value in graph[now]:
14+
nextCost = cost + value
15+
if nextCost < distance[next]:
16+
distance[next] = nextCost
17+
heapq.heappush(heap, [nextCost, next])
18+
return distance
19+
20+
V, E = map(int, input().split())
21+
K = int(input())
22+
graph = [[] for _ in range(V + 1)]
23+
for _ in range(E):
24+
u, v, w = map(int, input().split())
25+
graph[u].append((v, w))
26+
result=dikjstra(K)
27+
for i in range(1, len(result)):
28+
if result[i]==INF:
29+
print("INF")
30+
else:
31+
print(result[i])

0 commit comments

Comments
 (0)