Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5-janghw0126 #138

Merged
merged 3 commits into from
Sep 1, 2024
Merged

5-janghw0126 #138

merged 3 commits into from
Sep 1, 2024

Conversation

janghw0126
Copy link
Member

@janghw0126 janghw0126 commented Jul 31, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋ฐฑ์ค€ | ์ž‘์—…

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1์‹œ๊ฐ„ 30๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์˜ˆ์ „์— ์œ„์ƒ์ •๋ ฌ์„ ์ด์šฉํ•œ ๋ฌธ์ œ๋ฅผ ๋ฆฌ๋ทฐํ•œ ์ ์ด ์žˆ์—ˆ๋Š”๋ฐ, ์ด๋ฒˆ pr์— ํ’€ ๋ฌธ์ œ๋ฅผ ๊ณจ๋ผ๋ณด๋‹ค๊ฐ€ ์œ„์ƒ์ •๋ ฌ์„ ์ด์šฉํ•œ ๋ฌธ์ œ๋ฅผ ๋งˆ์นจ ๊ณ ๋ฅด๊ฒŒ ๋˜์–ด์„œ ์ด๋ฅผ ์ œ๋Œ€๋กœ ๊ณต๋ถ€ํ•ด๋ณด๊ณ ์ž ์ด ๋ฌธ์ œ๋ฅผ ํƒํ•˜์˜€์Šต๋‹ˆ๋‹ค!

๐Ÿ˜ฎ ์œ„์ƒ์ •๋ ฌ์ด๋ž€?

์ˆœ์„œ๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๋Š” ์ž‘์—…์„ ์ฐจ๋ก€๋กœ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ๋•Œ ๊ทธ ์ˆœ์„œ๋ฅผ ๊ฒฐ์ •ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๊ทธ๋ž˜ํ”„์˜ ํ๋ฆ„(์ˆœ์ฐจ์ ์ธ ๊ทธ๋ž˜ํ”„)์ด ์žˆ๊ณ  ๋‹ค์–‘ํ•œ ์กฐ๊ฑด๋“ค์ด ์„œ๋กœ ์–ฝํ˜€์žˆ์„ ๋•Œ, ์ •ํ™•ํžˆ 1์—ด๋กœ ๊ทธ๋ž˜ํ”„์˜ ๊ฐ ๋…ธ๋“œ๋ฅผ ๋‚˜์—ดํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ์œ„์ƒ์ •๋ ฌ์ž…๋‹ˆ๋‹ค. ์œ„์ƒ์ •๋ ฌ์€ DAG(Directed Acyclic Graph)์—๋งŒ ์ ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์‚ฌ์ดํด์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š” ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์ด๊ณ  ์‚ฌ์ดํด์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์œ„์ƒ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ด 3๊ฐœ์˜ ๊ณผ๋ชฉ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ด…์‹œ๋‹ค.
์„ธ ๊ณผ๋ชฉ์„ ๋ชจ๋‘ ๋“ฃ๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ -> ์•Œ๊ณ ๋ฆฌ์ฆ˜ -> ๊ณ ๊ธ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆœ์„œ๋กœ ๊ณผ๋ชฉ์„ ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๋งŒ์•ฝ ์ž๋ฃŒ๊ตฌ์กฐ -> ๊ณ ๊ธ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ -> ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆœ์„œ๋กœ ๊ณผ๋ชฉ์„ ๋“ฃ๋Š”๋‹ค๋ฉด, ํ•ด๋‹น ์ˆœ์„œ๋Š” ์˜ฌ๋ฐ”๋ฅธ ํ•™์Šต ์ˆœ์„œ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.

์œ„์ƒ ์ •๋ ฌ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋‹ต์ด ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐœ๋…์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

  • ์ง„์ž…์ฐจ์ˆ˜ : ํŠน์ •ํ•œ ๋…ธ๋“œ๊ฐ€ ์žˆ์„ ๋•Œ ๊ทธ ๋…ธ๋“œ๋กœ ๋“ค์–ด์˜ค๋Š” ๋‹ค๋ฅธ ๋…ธ๋“œ์˜ ๊ฐฏ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • ์ง„์ถœ์ฐจ์ˆ˜ : ํŠน์ •ํ•œ ๋…ธ๋“œ๊ฐ€ ์žˆ์„ ๋•Œ ๊ทธ ๋…ธ๋“œ์—์„œ ๋‚˜๊ฐ€๋Š” ๊ฐ„์„ ์˜ ๊ฐฏ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

โš™๏ธ ์œ„์ƒ์ •๋ ฌ ๋™์ž‘๊ณผ์ •

์œ„์ƒ์ •๋ ฌ ๋™์ž‘๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. ์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ธ ์ •์ ์„ ํ์— ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.
  2. ํ์—์„œ ์›์†Œ๋ฅผ ๊บผ๋‚ด ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ๊ฐ„์„ ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  3. ๊ฐ„์„  ์ œ๊ฑฐ ์ดํ›„์— ์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ด ๋œ ์ •์ ์„ ํ์— ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.
  4. ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ 2๋ฒˆ๊ณผ 3๋ฒˆ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ์›์†Œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ธฐ ์ „์— ํ๊ฐ€ ๋นˆ๋‹ค๋ฉด, ์‚ฌ์ดํด์ด ์กด์žฌํ•˜๋Š” ๊ฒƒ์ด๊ณ  ๋ชจ๋“  ์›์†Œ๋ฅผ ๋ฐฉ๋ฌธํ–ˆ๋‹ค๋ฉด ํ์—์„œ ๊บผ๋‚ธ ์ˆœ์„œ๊ฐ€ ์œ„์ƒ ์ •๋ ฌ์˜ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค.

์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ด๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.

โœ… ๋ฌธ์ œ ์ ‘๊ทผ ๋ฐฉ๋ฒ•

์ผ๋‹จ ๋ฌธ์ œ ์ง€๋ฌธ ์ค‘์—์„œ

์–ด๋–ค ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ˜๋“œ์‹œ ๋จผ์ € ์™„๋ฃŒ๋˜์–ด์•ผ ํ•  ์ž‘์—…๋“ค์ด ์žˆ๋‹ค.

์ด ๋ถ€๋ถ„์—์„œ ์œ„์ƒ ์ •๋ ฌ์„ ์ด์šฉํ•œ ๋ฌธ์ œ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

Note

๋ฌธ์ œ ๋กœ์ง

  1. ์ง„์ž… ์ฐจ์ˆ˜๊ฐ€ 0์ธ ์ž‘์—…๋“ค์„ ํ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ด๋ผ๋Š” ๊ฒƒ์€ ์„ ํ–‰ ์ž‘์—…์ด ์—†์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค!
  2. ํ๋ฅผ ์ด์šฉํ•ด์„œ ์œ„์ƒ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•˜๋ฉฐ ๊ฐ ์ž‘์—…์˜ ์™„๋ฃŒ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

โฌ‡๏ธ 2๋ฒˆ ๊ณผ์ •

  • ํ์—์„œ ์ž‘์—…์„ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด๋ฉด์„œ ํ•ด๋‹น ์ž‘์—…์„ ์„ ํ–‰ ์ž‘์—…์œผ๋กœ ๊ฐ€์ง€๋Š” ์ž‘์—…๋“ค์˜ ์™„๋ฃŒ ์‹œ๊ฐ„์„ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฐฑ์‹ ํ•  ๋•Œ๋Š”, ํ˜„์žฌ ์ž‘์—…์˜ ์™„๋ฃŒ ์‹œ๊ฐ„์— ํ•ด๋‹น ์ž‘์—…์˜ ์†Œ์š” ์‹œ๊ฐ„์„ ๋”ํ•ด์„œ ์™„๋ฃŒ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
  • ์ž‘์—… ์™„๋ฃŒ ์‹œ๊ฐ„์€ ์ตœ๋Œ€๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•˜์—ฌ ์ตœ์ข…์ ์œผ๋กœ ๋ชจ๋“  ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋Š” ์‹œ๊ฐ„์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•œ ํ›„ ์™„๋ฃŒ ์‹œ๊ฐ„ ๋ฐฐ์—ด์—์„œ ์ตœ๋Œ€๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ๋กœ์ง์„ ๋ฐ”ํƒ•์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.

โฌ‡๏ธ ์ตœ์ข… ์ฝ”๋“œ

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))

๐Ÿ˜ณ ๋‹ค๋ฅธ ํ’€์ด ๋ฐฉ๋ฒ•

์ œ ์ฝ”๋“œ๊ฐ€ ๋„ˆ๋ฌด ๋‚œ์žกํ•˜๊ณ  ๋ณต์žกํ•œ ๊ฒƒ ๊ฐ™์•„ ๋‹ค๋ฅธ ๋ถ„๋“ค์€ ์–ด๋–ป๊ฒŒ ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋Š”์ง€ ์•Œ์•„๋ณด๋Š” ์ค‘์— dp๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‘ผ ์ฝ”๋“œ๋“ค์ด ๋งŽ์ด ๋ณด์˜€์Šต๋‹ˆ๋‹ค.

import sys
input = sys.stdin.readline

n = int(input())
dp = [0]*(n+1)
for i in range(1, n+1):
    work, count, *pre = map(int, input().split())
    dp[i] = work
    for j in pre:
        dp[i] = max(dp[i], dp[j]+work)
print(max(dp))

์ด๋ ‡๊ฒŒ ๊ฐ„๊ฒฐํ•  ์ˆ˜๊ฐ€...๐Ÿคง
์ด ์ฝ”๋“œ๋Š” ๋™์  ๊ณ„ํš๋ฒ•์„ ํ†ตํ•ด ์„ ํ–‰ ์ž‘์—… ๊ด€๊ณ„๋ฅผ ์ฒ˜๋ฆฌํ•˜์—ฌ ์ž‘์—… ์™„๋ฃŒ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
์ฆ‰, ์„ ํ–‰์ž‘์—…์ด ์—†๋‹ค๋ฉด 0์‹œ๊ฐ„๋ถ€ํ„ฐ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ž‘์—…์‹œ๊ฐ„์„ ๊ทธ๋Œ€๋กœ ์ €์žฅํ•˜๊ณ , ์„ ํ–‰์ž‘์—…์ด ์žˆ๋‹ค๋ฉด ๊ฐ€์žฅ ๋Šฆ๊ฒŒ ๋๋‚˜๋Š” ์„ ํ–‰์ž‘์—…์— ์ž‘์—…์‹œ๊ฐ„์„ ๋”ํ•˜์—ฌ ์ €์žฅํ•˜๋Š” ๋กœ์ง์„ ์‚ฌ์šฉํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์œ„์ƒ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ฐœ๋…์„ ์ž์„ธํžˆ ๊ณต๋ถ€ํ–ˆ์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ , ์ฒ˜์Œ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ๊ตฌํ˜„ํ•˜๋Š” ๊ณผ์ •์ด ์–ด๋ ต๊ณ  ์‰ฝ์ง€ ์•Š์•„์„œ ๋‹ค๋ฅธ ๋ถ„๋“ค์˜ ์ฝ”๋“œ์™€ ์ €์˜ ๋‹จ์ง์นœ๊ตฌ์ธ ์ฑ—์ง€ํ”ผํ‹ฐ..ใ…Ž ์˜ ๋„์›€์„ ๋ฐ›์•„ ๊ฒจ์šฐ ํ•ด๊ฒฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์•ž์œผ๋กœ ์œ„์ƒ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊นจ๋ถ€์ˆ˜๊ธฐ ์œ„ํ•ด์„œ ๋” ๋งŽ์ด ๊ณต๋ถ€ํ•˜๊ณ  ๋ฌธ์ œ๋ฅผ ๋งŽ์ด ํ’€์–ด๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค!๐Ÿ˜…๐Ÿ˜‚๐Ÿคฃ๐Ÿฅฒ

@jung0115
Copy link
Member

jung0115 commented Aug 4, 2024

์ €๋Š” ์œ„์ƒ์ •๋ ฌ ๋ฌธ์ œ ๋‚˜์˜ฌ ๋•Œ๋งˆ๋‹ค ์–ด๋–ป๊ฒŒ ํ’€์–ด์•ผ ํ• ์ง€ ํ—ท๊ฐˆ๋ ธ์—ˆ๋Š”๋ฐ ํ˜œ์›๋‹˜ ํ’€์ด ๋•๋ถ„์— ๊ฐœ๋…์ด ์ข€ ์žกํžŒ ๊ฑฐ ๊ฐ™์•„์š”!! ๋งˆ์ง€๋ง‰์— dp ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•˜๊ธด ํ•˜์ง€๋งŒ ํ’€์ด ์ดํ•ด๊ฐ€ ์ž˜ ๋˜๋Š” ๊ฑด ํ˜œ์› ๋‹˜ ์ฝ”๋“œ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค โ˜บ๏ธ

์œ„์ƒ์ •๋ ฌ ๋ฌธ์ œ๋ฅผ ๋ช‡ ๊ฐœ ํ’€์–ด๋ณด๋ฉด์„œ ์ต์ˆ™ํ•ด์ง€๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ ธ๋ณด๋ ค๊ตฌ ํ•ฉ๋‹ˆ๋‹น ๐Ÿ‘

+)

์–ด๋–ค ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ˜๋“œ์‹œ ๋จผ์ € ์™„๋ฃŒ๋˜์–ด์•ผ ํ•  ์ž‘์—…๋“ค์ด ์žˆ๋‹ค.

์ด ๋ถ€๋ถ„์—์„œ ์œ„์ƒ ์ •๋ ฌ์„ ์ด์šฉํ•œ ๋ฌธ์ œ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ €๋Š” ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ์œ„์ƒ ์ •๋ ฌ ๋ฌธ์ œ์ธ์ง€ ํŒ๋‹จํ•˜๋Š” ๊ฒƒ๋„ ์–ด๋ ค์›€์ด ์žˆ์—ˆ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ์ •๋ฆฌํ•ด์ฃผ์…”์„œ ๋ญ”๊ฐ€ ๊ฐ€๋‹ฅ์ด ์žกํ˜”์–ด์š”!

Copy link
Collaborator

@wonjunYou wonjunYou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ค.. ์œ„์ƒ ์ •๋ ฌ์€ ์ฒ˜์Œ ์ ‘ํ•ด๋ณด๋Š”๋ฐ ์™„์ „ ์ƒˆ๋กœ์›Œ์š”! ์ˆœ์„œ๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๋Š” ์ž‘์—…์„ ์ฐจ๋ก€๋กœ ์ˆ˜ํ–‰ํ•  ๋•Œ ์ˆœ์„œ๋ฅผ ์ •ํ•ด์ฃผ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ์ด๋Ÿฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์žˆ์—ˆ๋‹ค๋‹ˆ..

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ๋ชจ๋“  ์ž‘์—…์„ ์™„๋ฃŒํ•˜๊ธฐ๊นŒ์ง€ ์ตœ์†Œ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฌธ์ œ์—ฌ์„œ DP๋กœ ํ’€์–ด๋‚ผ ์ˆ˜ ์žˆ์—ˆ๋Š”๋ฐ, ์•„์˜ˆ ์ž‘์—… ๊ฐ„์˜ ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์œ„์ƒ ์ •๋ ฌ์„ ์จ์•ผ ํ•˜๊ฒ ๊ตฐ์š”..!! ์ด๋ฒˆ PR๋„ ๋„ˆ๋ฌด ๊ณ ์ƒํ•˜์…จ์–ด์š” ~~

DP๋กœ ํ’€์ดํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค! ์ ์–ด์ฃผ์‹  DP ์ฝ”๋“œ์™€ ์—„์ฒญ ๋น„์Šทํ•˜๋„ค์š”!!

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(br.readLine());

        int[] dp = new int[n + 1];

        int time;
        int preTaskCount;

        for (int i = 1; i < n + 1; i++) {
            int[] taskInfo = Arrays.stream(br.readLine().split(" "))
                .mapToInt(Integer::parseInt)
                .toArray();

            time = taskInfo[0];
            preTaskCount = taskInfo[1];

            dp[i] = time;

            for (int j = 2; j < taskInfo.length; j++) {
                dp[i] = Math.max(dp[i], dp[taskInfo[j]] + time);
            }

        }

        int result = Arrays.stream(dp)
            .max()
            .getAsInt();

        System.out.println(result);
    }
}

@LJEDD2
Copy link
Collaborator

LJEDD2 commented Aug 29, 2024

5์ฐจ์‹œ๊ฐ€ ๊ณ ๋‚œ์˜ ํ•œ์ฃผ์˜€๊ตฐ์š” ... !
๋‹ค๋“ค ์‚ฌ์šฉํ•˜์‹  ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆ˜์ค€์ด ์—„์ฒญ๋‚ฉ๋‹ˆ๋‹ค,...!!
๊ฒ๋งŒ ๋จน๊ณ  ๋งจ๋‚  ์ณ๋‹ค๋ณด์ง€๋„ ์•Š๋˜ ์œ„์ƒ์ •๋ ฌ์„ ํ˜œ์›๋‹˜ ๋•๋ถ„์— ๋‹ค์‹œ ํ•œ ๋ฒˆ ์ณ๋‹ค๋ณด๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค (๋งค์šฐ ๋ฐ˜์„ฑํ•ฉ๋‹ˆ๋‹ค)

์œ„์ƒ์ •๋ ฌ์ด๋ผ๋Š” ์ด๋ฆ„๊ณผ ์–ด๋–ค ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ˜๋“œ์‹œ ๋จผ์ € ์™„๋ฃŒ๋˜์–ด์•ผ ํ•  ์ž‘์—…์ด๋ผ๋Š” ์„ค๋ช…์„ ์ฝ๊ณ 
ํ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•˜๋‚˜? ๊นŒ์ง€ ๋– ์˜ฌ๋ ค๋ณผ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค

ํ˜œ์›๋‹˜์˜ ์ฝ”๋“œ ํ•˜๋‚˜ํ•˜๋‚˜ ๋”ฐ๋ผ๊ฐ€๋ณด๋ฉฐ ์œ„์ƒ์ •๋ ฌ์„ ์ฐ๋จนํ•ด๋ณผ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค ๐Ÿ˜Ž
๊ทธ๋ž˜ํ”„์˜ ์ˆœ์„œ ํ๋ฆ„๊ณผ ๋‹ค์–‘ํ•œ ์กฐ๊ฑด๋“ค์ด ์„œ๋กœ ์–ฝํ˜€์žˆ์„ ๋•Œ, ๊ทธ๋ž˜ํ”„์˜ ๊ฐ ๋…ธ๋“œ๋ฅผ ๋‚˜์—ดํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๋Š” ๊ฒƒ์„ ํ•˜๋‚˜ ๋” ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ’ชโ˜˜๏ธ

Comment on lines +21 to +24
queue = deque()
for i in range(1, n + 1):
if indegree[i] == 0:
queue.append(i)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ์กฐ๊ฑด๋ฌธ๋„ ๋ฆฌ์ŠคํŠธ์ปดํ”„๋ฆฌํ•ธ์…˜์ด ๋˜๋„ค์š” ... ๐Ÿ˜Ž

queue = deque([i for i in range(1, n + 1) if indegree[i] == 0])

@janghw0126 janghw0126 merged commit 2bf872f into main Sep 1, 2024
@janghw0126 janghw0126 deleted the 5-janghw0126 branch September 1, 2024 05:50
@LJEDD2 LJEDD2 mentioned this pull request Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants