Skip to content

Commit 81f09af

Browse files
authored
Merge pull request #227 from AlgorithmStudy-Allumbus/YoonYn9915
YoonYn9915 / 6월 4주차 / 3문제
2 parents 745e6c7 + 685ff9b commit 81f09af

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import sys
2+
import heapq
3+
4+
input = sys.stdin.readline
5+
INF = float('inf')
6+
7+
N, E = map(int, input().split())
8+
graph = [[] for _ in range(N + 1)]
9+
for _ in range(E):
10+
a, b, c = map(int, input().split())
11+
graph[a].append((b, c))
12+
graph[b].append((a, c))
13+
v1, v2 = map(int, input().split())
14+
15+
16+
17+
def dijkstra(start, end):
18+
dist = [INF] * (N + 1)
19+
dist[start] = 0
20+
hq = [(0, start)]
21+
while hq:
22+
len, node = heapq.heappop(hq)
23+
if len > dist[node]:
24+
continue
25+
for next_node, val in graph[node]:
26+
if dist[next_node] > dist[node] + val:
27+
dist[next_node] = dist[node] + val
28+
heapq.heappush(hq, (dist[next_node], next_node))
29+
return dist[end]
30+
31+
32+
path1 = dijkstra(1, v1) + dijkstra(v1, v2) + dijkstra(v2, N)
33+
path2 = dijkstra(1, v2) + dijkstra(v2, v1) + dijkstra(v1, N)
34+
35+
print(-1) if path1 >= INF and path2 >= INF else print(min(path1, path2))
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import heapq
2+
import sys
3+
4+
input = sys.stdin.readline
5+
6+
V, E = map(int, input().split())
7+
start = int(input())
8+
graph = [[] for _ in range(V + 1)]
9+
10+
for _ in range(E):
11+
u, v, w = map(int, input().split())
12+
13+
graph[u].append((v, w))
14+
15+
16+
def dijkstra(start):
17+
distances = [float("inf")] * (V + 1)
18+
distances[start] = 0
19+
q = []
20+
heapq.heappush(q, (distances[start], start))
21+
22+
while q:
23+
cnt_distance, node = heapq.heappop(q)
24+
25+
if distances[node] < cnt_distance:
26+
continue
27+
28+
for adjacency_node, distance in graph[node]:
29+
cal_distance = distances[node] + distance
30+
31+
if cal_distance < distances[adjacency_node]:
32+
distances[adjacency_node] = cal_distance
33+
heapq.heappush(q, (cal_distance, adjacency_node))
34+
35+
return distances
36+
37+
38+
result = dijkstra(start)
39+
40+
for i in range(1, len(result)):
41+
print("INF" if result[i] == float("inf") else result[i])
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
N = int(input())
2+
3+
arr = []
4+
5+
for _ in range(N):
6+
q, p = list(map(int, input().split()))
7+
arr.append([q, p])
8+
9+
arr.sort(reverse=True)
10+
11+
count = 0
12+
for i in range(5, len(arr)):
13+
if arr[4][0] == arr[i][0]:
14+
count += 1
15+
else:
16+
break
17+
18+
print(count)

0 commit comments

Comments
 (0)