Jedi Academy
Time limit2sMemory limit512 MB
Given a DAG of skill prerequisites and two buildings, find the shortest total time to learn all skills, counting travel and learning time.
- Level
Medium7 of 10
- Topics
- Graph, Dynamic programming, Topological sort, Greedy
- Solved
- No attempts yet
Problem
Becoming a Jedi requires mastering many theoretical and practical skills. At the Jedi Academy you can learn everything you need, provided you have the talent.
The academy's new student Phil is very talented and studies individually. He is free to build his own schedule. Phil is curious, so he devotes all his time to studying. The only moments Phil is not studying are when he moves from one academy building to the other or walks from the dormitory to one of the buildings. The academy has two buildings: one teaches theoretical skills and the other teaches practical skills. Traveling from one building to the other takes exactly minutes. Learning any skill takes exactly minutes. Initially Phil is in the dormitory, and the trip from the dormitory to either building also takes minutes.
Naturally, skills cannot be learned in an arbitrary order. For example, before mastering a lightsaber you must first learn the basics of optics and the art of unarmed combat. Phil has to take this into account when building his schedule.
Phil wants to become a Jedi as soon as possible, but to do so he must learn all the necessary skills. Before starting his studies, he wants to know the minimum number of minutes in which he can learn all the skills. Help him find out. After finishing his studies Phil immediately sets off to fight evil, so he does not need to return to the dormitory.
Input
The first line of the input contains an integer , the number of skills to be learned at the academy (). All skills are numbered from 1 to .
The next lines describe the requirements for learning each skill. At the start of the -th of these lines there is the number 1 or 2, indicating in which building the -th skill can be learned. Then comes the number , the number of skills required to learn skill . Then, on the same line, come integers, the skills required to learn skill . The sum of the numbers over all skills does not exceed .
The next line contains two integers: , the time in minutes to travel from one building to the other or from the dormitory to a building, and , the time to learn one skill ().
There exists an order of learning all the skills such that whenever a skill is learned, all the skills required for it have already been learned.
Output
In a single line of the output, print the minimum time in minutes Phil needs to become a Jedi.