ACM Computer Factory
Time limit1sMemory limit128 MB
Given machines with part-mask inputs, outputs, and hourly capacities, find the maximum factory throughput from the empty state to the full state.
- Level
Medium7 of 10
- Topics
- Graph, BFS, Implementation
- Solved
- No attempts yet
Problem
All computers used in a programming contest must be identical so that every participant competes on equal terms, so they are all built at a single factory.
Every computer consists of parts. A computer is complete once all parts are present, and only then can it be shipped.
Assembly is fully automated by machines. A machine takes a partially assembled computer, removes some parts, and adds others (removal is sometimes necessary because parts cannot always be added in an arbitrary order). Each machine has a performance (how many computers it can process per hour), an input specification, and an output specification.
The input specification is a list of values, each , , or : for part , means the part must be absent, means it must be present, and means its presence does not matter. A machine can process a partially assembled computer only if every part matches the machine's input specification.
The output specification is a list of values, each or : after the machine finishes, part is absent when the value is and present when it is , regardless of its previous state.
Machines are linked by production lines whose transfer time is negligible compared with processing time. A blank computer (all parts absent) is fed in, is processed by a sequence of machines, and leaves complete (all parts present).
You may route the partially assembled computers between machines in any way, provided that no machine processes more than computers per hour. Under the best possible routing, how many complete computers can the factory produce per hour?
Input
The first line contains two integers and .
Each of the next lines describes one machine using integers: , followed by the input specification , followed by the output specification . Here is the performance of machine , is its input specification for part , and is its output specification for part .
Output
Print a single integer: the maximum number of complete computers the factory can produce per hour under the best routing of the production lines. If no complete computer can be produced, print .
Constraints
- , ,