t_커리큘럼_303

source github.com/ndb796/python-for-coding-test
type 📌 개발노트
topics 600-알고리즘 & 코딩테스트 608 위상정렬
types 문제풀이

문제

강의 1부터 n까지. 도잇에 여러강의 들을 수 있고 일부는 선수강의가 있다.
n개 강의에 대하여 수강하기까지 걸리는 최소의 시간을 각각출력

from collections import deque

input = input.strip().split('\n')
n = int(input[0])
times = [0]*(n+1)
q = []
# <span id="진입차수가-0이다-강의를-들어도된다"></span>진입차수가 0이다. = 강의를 들어도된다.
front = [[] for _ in range(n+1)]
for i in range(1,n+1):
    t,*arr,z = list(map(int,input[i].split()))
    times[i] = t
    front[i]= arr
    if len(arr) == 0 :
        q.append(i)
        
while q:
    nq = []
    syn = [[] for _ in range(n+1)]
    # q는 진입차수 0인거
    for i in q:
        for j in range(1,n+1):
            leng = len(front[j])
            if leng != 0 and i in front[j]:
                # 들은과목 들어야하는 과목에서빼준다..
                syn[j].append(times[i])
                if leng == 1:
                    nq.append(j)
                    times[j] += max(syn[j])
                front[j].remove(i)
    q = nq
    
for i in range(1,n+1):
    print(times[i])

나는 while문에서 진입차수가 0인 즉 동시에 실행할 수있는 과목중에서
특정 과목의 선수과목중에 윗줄에서 말한 과목이 있으면 리스트에 저장했다가
해당과목을 들어야할때 즉 진입차수가 0일때 동시에 들어야할 과목중에 max를 선택하는식으로 짯다.

책에서는
times를 deepcopy하여
deepcopy를 하기 위해서는 import copy copy.deepcopy(list)를 이용하여 할 수 있다.

result[i] = max(result[i], result[now] + time[i])

이런식으로 해도됨.