Money Copying
Time limit2sMemory limit1024 MB
Given barter trades between items and won, find the minimum starting money that lets a holder grow money without bound, or INF if none exists.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Binary search
- Solved
- No attempts yet
Problem
The GIST House secondhand market runs events that exchange money for a specific item, or one specific item for another specific item. When items are swapped, extra money may also change hands. The market has unlimited money and items, so any trade can be repeated indefinitely as long as goods remain. However, a customer can hold at most one item at a time.
For example, suppose the following trades exist:
- : A user buys item for won.
- : A user adds won and trades it for item .
- : A user sells item and receives won.
- : A user trades item for item and receives an additional won.
- : A user buys item for won.
Suppose the development team allows only the trades in the list . Then choosing in this order turns won into won. Repeating this indefinitely lets the money grow without bound. Copied money can harm the national economy, so the development team wants to prevent this. Given a list of trades, the team wants to determine whether the list is safe.
The safety level of a list is defined as follows.
- If a person starts with won and can grow their money without bound by repeating trades, the safety level of the list is the minimum value of . If no such exists, the safety level is . To grow the money, it is not necessary to sell every item.
For example, the safety level of is , because starting with won and repeating in order works. However, a person with won cannot grow their money without bound no matter what they do. The safety level of is .
Meanwhile, the safety level of is . It seems that repeating and would grow the money without bound, but there is no way to obtain item or item , so those trades cannot be attempted.
Write a program that, given a list of barter trades, measures the safety level of that list.
Input
The first line contains the number of items and the length of the barter trade list . ()
From the second line, the next lines each give one piece of information about the trade list in one of the following five formats.
- : You can buy item for won.
- : You can sell item and earn won.
- : You can trade item for item .
- : You can exchange item and won for item .
- : You can exchange item for item and won.
, , and are integers from to . is an integer from to . The values on each line are separated by spaces.
In the trade list , formats , , and each satisfy .
No two lines of format share the same .
No two lines of format share the same .
No two lines of formats , , or share both the same and . For example, 3 1 2 and 4 1 1 2 are never given together. However, 3 1 2 and 3 1 3 can be given together, because they share but not .
Trades are not reversible. For example, the input 3 1 2 does not mean you can trade item for item .
Output
Print the safety level of the list. If the safety level is , print the string INF.