코딩테스트/BOJ

[백준] [다익스트라 알고리즘] 14938. 서강그라운드

박소민 2023. 5. 9. 10:20
14938. 서강그라운드
 

14938번: 서강그라운드

예은이는 요즘 가장 인기가 있는 게임 서강그라운드를 즐기고 있다. 서강그라운드는 여러 지역중 하나의 지역에 낙하산을 타고 낙하하여, 그 지역에 떨어져 있는 아이템들을 이용해 서바이벌을

www.acmicpc.net

 

  • 내 풀이
    • 전형적인 다익스트라 문제
#아이템 몇개있는지
#양방향
#지역개수n, 수색범위m, 길 개수 r
import heapq

n,m,r=map(int,input().split())
item=list(map(int,input().split()))
graph=[[] for _ in range(n+1)]


answer=0

def solve(start):
    q=[]
    result = [int(1e9) for _ in range(n + 1)]
    heapq.heappush(q, (0,start))
    result[start]=0

    while q:
        v,x=heapq.heappop(q)

        for gp in graph[x]:
            time, end=gp
            if end == start: continue
            if result[end]>v+time:
                result[end]=v+time
                heapq.heappush(q, (result[end], end))

    count(result)

def count(result):
    global answer
    tmp=0
    for i in range(1,n+1):
        if result[i]<=m:
            tmp+=item[i-1]

    answer=max(answer,tmp)



for i in range(r):
    a,b,t=map(int,input().split())
    graph[a].append((t,b))
    graph[b].append((t,a))


for i in range(1,n+1):
    solve(i)

print(answer)