Rock Man
InterviewTime limit8sMemory limit512 MB
Each tool takes day1 days normally or day2 if its support tool is finished first; order the tools to minimize total days.
- Level
Medium6 of 10
- Topics
- Graph, Greedy, Implementation, Sorting
- Solved
- No attempts yet
Problem
You were a well known craftsman in the stone age, and you spent your life carving tools out of natural stone. Your works are still displayed with respect in museums in 2006. Most visitors, though, do not linger in front of them. Watching that from Heaven, you remembered the days you had worked yourself to the bone.
On one of your busiest days you received a number of orders for tools. Each order asked for a different tool. Filling a single order from bare stone often took several days. But a tool you had already finished let you carve a new tool faster. For every tool in the order list there is exactly one tool that can support carving it.
Carving one tool takes one of two amounts of time. With no support tool it takes days. If the support tool is already finished when you start, it takes only days. Some tools list themselves as their own support tool, and such a tool cannot support itself.
You carve one tool at a time, so the whole schedule lasts the sum of the per tool day counts. Find the smallest number of days needed to fill every order.
Input
The input is a series of test cases. Each case begins with a line holding a single integer , the number of ordered tools.
Then lines follow. Each line specifies one order with four values separated by one or more spaces, in the order , , , . is the name of the tool in that order, is the number of days needed to make the tool without any support tool, is the name of the tool that can support carving it, and is the number of days needed to make the tool after is finished.
The input ends with the case where . Do not process that case.
- has at most 32 non-space characters and is unique within a test case
- is one of the names given in the same test case, and it may be itself
Output
For each test case, print one line holding the minimum number of days required to fill all orders.