Milk Scheduling
InterviewTime limit1sMemory limit128 MB
Given task durations and precedence constraints that form a DAG, find the minimum makespan when unlimited workers milk cows in parallel.
- Level
Medium5 of 10
- Topics
- Graph, Topological sort, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
Farmer John's cows () are numbered through . Milking cow takes units of time. Because of the layout of the barn, some cows must be milked before others: if cow must be milked before cow , then John must completely finish milking before he can start milking .
To finish as quickly as possible, John has hired enough farmhands to milk any number of cows at the same time. Even so, the ordering constraints limit how fast the whole process can go. Compute the minimum total time needed to milk all of the cows.
Input
- Line 1: Two space-separated integers (the number of cows) and (the number of milking constraints, ).
- Lines 2 through : Line contains ().
- Lines through : Each line contains two space-separated integers and , meaning that cow must be completely milked before cow can be started. These constraints never form a cycle, so a solution always exists.
Output
- Line 1: The minimum amount of time required to milk all of the cows.
Hint
In the first example there are cows, and milking each of them takes , , and units of time respectively. Cow must be completely milked before cow can start.
Cows and can be milked at the same time from the beginning. Once cow is finished, cow can start. All cows finish being milked after units of time.