10282. 해킹
10282번: 해킹
최흉최악의 해커 yum3이 네트워크 시설의 한 컴퓨터를 해킹했다! 이제 서로에 의존하는 컴퓨터들은 점차 하나둘 전염되기 시작한다. 어떤 컴퓨터 a가 다른 컴퓨터 b에 의존한다면, b가 감염되면
www.acmicpc.net
- 내 풀이
- 다익스트라: heap 써야함
- 계속 틀린 이유
- 더 짧은 시간값이 나오면 그와 연결된 값들도 다 업데이트 해줘야하므로 힙에 다시 넣어줘야함!!
# a->b 의존 b감염->a감염
import heapq
def bfs(start):
global result
global cnt
visited.add(start)
result[start]=0
cnt+=1
heapq.heappush(q, (0,start))
while q:
time, x =heapq.heappop(q)
for t,g in graph[x]:
if g not in visited:
cnt+=1
visited.add(g)
if result[g]>time+t:
result[g] = time + t
#더 짧은 시간값이 나오면 그와 연결된 값들도 다 업데이트 해줘야하므로
#힙에 다시 넣어줘야한다
heapq.heappush(q, (result[g],g))
T = int(input())
for tc in range(T):
# 컴퓨터수, 의존성 수, 해킹컴 번호(시작)
n, d, c = map(int, input().split())
graph = [[] for i in range(n + 1)]
cnt = 0
result = [int(1e9) for _ in range(n + 1)]
visited = set()
q=[]
for i in range(d):
# a->b 의존 s초후 감염
a, b, s = map(int, input().split())
graph[b].append((s,a))
bfs(c)
tmp = 0
answer = 0
# result=[result[i] if i in visited else 0 for i in range(n+1)]
answer=0
for r in result:
if r!=int(1e9):
answer=max(answer,r)
print(cnt, answer)'코딩테스트 > BOJ' 카테고리의 다른 글
| [백준] [DP] [이진탐색] 1965. 상자 넣기 (0) | 2023.05.13 |
|---|---|
| [백준] [최단경로] [다익스트라] [플로이드워셜] 11265. 끝나지 않는 파티 (0) | 2023.05.13 |
| [백준] [DP] 17212. 달나라 토끼를 위한 구매대금 지불 도우미 (0) | 2023.05.10 |
| [백준] [부분집합] 6603. 로또 (0) | 2023.05.09 |
| [백준] [다익스트라 알고리즘] 14938. 서강그라운드 (0) | 2023.05.09 |