Skip to content

Commit d4e8fb4

Browse files
committed
서강그라운드 풀이
1 parent 3edb560 commit d4e8fb4

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

BAEKJOON/3Gold/서강그라운드.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 그래프 이론, 데이크스트라, 최단 경로, 플로이드–워셜
2+
# https://www.acmicpc.net/problem/14938
3+
4+
# 두 지역 간에 더 짧은 경로가 있을 수 있음.
5+
#
6+
7+
def dijkstra(items, graph, M, start):
8+
import heapq
9+
10+
INF = 0xFFFFFFFF
11+
dist = [INF for _ in range(N+1)]
12+
dist[start] = 0
13+
heap = [(0, start)]
14+
15+
while heap:
16+
cost, node = heapq.heappop(heap)
17+
if cost > dist[node]: continue
18+
19+
for _node, _cost in graph[node]:
20+
sum_cost = dist[node] + _cost
21+
if sum_cost >= dist[_node]: continue
22+
if sum_cost > M: continue
23+
dist[_node] = sum_cost
24+
heapq.heappush(heap, (sum_cost, _node))
25+
26+
total_item = 0
27+
for i in range(1, N+1):
28+
if dist[i] != INF:
29+
total_item += items[i]
30+
return total_item
31+
32+
if __name__ == "__main__":
33+
from collections import defaultdict
34+
input = __import__("sys").stdin.readline
35+
36+
N,M,R = map(int, input().split())
37+
items = [None] + list(map(int, input().split()))
38+
graph = defaultdict(list)
39+
for _ in range(R):
40+
a,b,l = map(int, input().split())
41+
graph[a].append((b,l))
42+
graph[b].append((a,l))
43+
44+
max_item = 0
45+
for n in range(1, N+1):
46+
item = dijkstra(items, graph, M, n)
47+
if max_item < item:
48+
max_item = item
49+
50+
print(max_item)

0 commit comments

Comments
 (0)