Skip to content

Commit

Permalink
Merge pull request #138 from AlgoLeadMe/5-janghw0126
Browse files Browse the repository at this point in the history
5-janghw0126
  • Loading branch information
janghw0126 authored Sep 1, 2024
2 parents ef09710 + e49088f commit 2bf872f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
3 changes: 2 additions & 1 deletion janghw0126/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@
| 2차시 | 2024.7.21 | 그리디 | <a href= "https://www.acmicpc.net/problem/1931">회의실 배정</a> |[#127](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/127) |
| 3차시 | 2024.7.24 || <a href= "https://www.acmicpc.net/problem/5430">AC</a> |[#130](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/130) |
| 4차시 | 2024.7.28 | 분할 정복 | <a href= "https://www.acmicpc.net/problem/1074">Z</a> |[#135](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/135) |
---
| 5차시 | 2024.7.31 | 위상정렬 | <a href= "https://www.acmicpc.net/problem/2056">작업</a> |[#138](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/138) |
---
4 changes: 2 additions & 2 deletions janghw0126/덱/5430.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
else:
# 정상 상태에서는 덱의 앞에서 제거
dq.popleft()

if error_occurred:
continue # 에러가 발생하면 다음 테스트 케이스로 넘어감

if is_reversed:
# 최종 상태가 뒤집힌 경우 덱을 뒤집음
dq.reverse()

# 덱을 문자열로 변환하여 출력
print("[" + ",".join(dq) + "]")
49 changes: 49 additions & 0 deletions janghw0126/위상정렬/2056.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import sys
from collections import deque
input = sys.stdin.readline

def topological_sort_and_compute_times(n, task_times, dependencies):
# 그래프와 진입 차수 초기화
graph = {i: [] for i in range(1, n + 1)}
indegree = [0] * (n + 1)
completion_time = [0] * (n + 1)

# 작업 시간 초기화
for i in range(1, n + 1):
completion_time[i] = task_times[i]

# 그래프와 진입 차수 설정
for a, b in dependencies:
graph[a].append(b)
indegree[b] += 1

# 진입 차수가 0인 노드를 큐에 추가
queue = deque()
for i in range(1, n + 1):
if indegree[i] == 0:
queue.append(i)

# 위상 정렬과 작업 완료 시간 계산
while queue:
current = queue.popleft()
for neighbor in graph[current]:
completion_time[neighbor] = max(completion_time[neighbor], completion_time[current] + task_times[neighbor])
indegree[neighbor] -= 1
if indegree[neighbor] == 0:
queue.append(neighbor)

return max(completion_time[1:])

n = int(input().strip())
task_times = [0] * (n + 1)
dependencies = []

# 작업 시간 입력
for i in range(1, n + 1):
data = list(map(int, input().split()))
task_times[i] = data[0]
m = data[1]
for j in range(m):
dependencies.append((data[2 + j], i))

print(topological_sort_and_compute_times(n, task_times, dependencies))

0 comments on commit 2bf872f

Please sign in to comment.