Thief of Wealth
Published 2020. 10. 2. 12:04
10282 해킹 개발/알고리즘

www.acmicpc.net/problem/10282

 

10282번: 해킹

최흉최악의 해커 yum3이 네트워크 시설의 한 컴퓨터를 해킹했다! 이제 서로에 의존하는 컴퓨터들은 점차 하나둘 전염되기 시작한다. 어떤 컴퓨터 a가 다른 컴퓨터 b에 의존한다면, b가 감염되면 ��

www.acmicpc.net

 

다익스트라 문제인데 약간의 변형이 있다.

 

핵심은 다음과 같다.

1. a,b,s로 주어져도 의존성정의에 의해 b->a로 가능 비용이 s이다. 평소처럼 a->b가 s라고 생각하면 안된다.

2. INF가 아닌 값들을 카운트하면 총 감염된 컴퓨터 개수가 나온다.

3. INF를 제외한 distance에서 가장 큰값이 가장 마지막에 감염된 컴퓨터시간으로 정의하면 된다.

 

import sys
input = sys.stdin.readline

import heapq

T = int(input())

INF = int(1e9)

def countHacked(arr):
  return len(list(filter(lambda x: x!=-1, arr)))

answer = []

for _ in range(T):
  n,d,start = map(int, input().rstrip().split(" "))
  info = dict()
  visited = [0] * (1+n)
  distance = [INF] * (1+n)

  for i in range(1, n+1):
    info[i] = []
  
  for __ in range(d):
    b,a,s = map(int, input().rstrip().split(" "))
    info[a].append( (b,s) )
  

  q = []
  heapq.heappush(q, (0, start))
  distance[start] = 0
  
  while( len(q) != 0 ):
    weight, curr = heapq.heappop(q)
    visited[curr] = 1
    
    for i in info[curr]:
      dest, cost = i
      if( visited[dest] == 0 ):
        if( distance[dest] > weight + cost):
          distance[dest] = weight+cost
          heapq.heappush(q, (distance[dest], dest))
  
  for i in range(len(distance)):
    if(distance[i] == INF):
      distance[i] = -1
  
  answer.append( (countHacked(distance), max(distance) ))

for i in answer:
  print(" ".join(map(str, i)))

'개발 > 알고리즘' 카테고리의 다른 글

1931 회의실배정  (0) 2020.10.03
11047 동전 0  (0) 2020.10.02
2665 미로만들기  (0) 2020.09.30
11404 플로이드  (0) 2020.09.29
1389 케빈 베이컨의 6단계 법칙  (0) 2020.09.29
profile on loading

Loading...