## APIO '17 P2 - Travelling Merchant

View as PDF

Points: 20 (partial)
Time limit: 1.0s
Memory limit: 1G

Problem types

After many days' journey through the great Australian outback, you have finally arrived at the great city of Cobar with nothing but a small backpack. Enamoured by the wonder and beauty of its markets, you decide to become a merchant and make Cobar your new home. Cobar has markets, numbered from to , connected by one-way footpaths, each taking a particular number of minutes to walk along.

The markets of Cobar trade in different items, numbered from to . Each market has a fixed price for buying or selling each item. Not every market will trade in every item, and it is possible that for any given item, a market may only support buying it, but not selling it, or vice-versa. You may assume that each market that has a given item for sale has an infinite quantity of it available, and similarly, if a market wants to buy a given item, it is willing to buy it repeatedly, forever.

To earn money as quickly as possible, you would like to find the most efficient profit cycle. A profit cycle is a walk through Cobar that starts at some market with your backpack empty, continues along Cobar's footpaths and through its markets (possibly buying and selling items along the way), and finally returns to , again with an empty backpack. It may visit a market and/or walk along a footpath multiple times. Once an item is bought, it must be placed immediately in your backpack and since your backpack is small, it can only hold up to a single item at any point in time. You may assume that you are always able to buy an item if it is available, regardless of the amount of money you possess so far and that you are prohibited from selling an item that you do not possess.

The profit earned from such a cycle is the total amount of money you earned from selling items, minus the total amount of money you spent buying them. The duration of a cycle is the total number of minutes you spend walking along its constituent footpaths. The efficiency of a profit cycle is the profit earned from it, divided by its duration. Note that a profit cycle that does not involve buying or selling any items has an efficiency of .

Your task is to find the maximum efficiency among all profit cycles with strictly positive duration. You should report this value rounded down, to the nearest integer. If no such profit cycle exists, you should report 0.

#### Input

The first line will contain integers, , and : the number of markets, footpaths and items respectively.

lines follow. The th of these lines will contain integers describing a market. For all , the pair of integers and describe the price at which you can buy and sell item at market , respectively. If an item cannot be bought or sold, then will be used as a placeholder.

lines follow. The th of these will contain integers, , and , describing a one-way footpath from market to another market , taking minutes to traverse.

#### Output

Your program should write to standard output.

Print a single integer, the maximum efficiency among all profit cycles, rounded down to the nearest integer.

For all subtasks, , and for all items which can be bought/sold, for all and for all .

Additionally, and for all , and there does not exist any pair of edges such that .

for all and for all . You can only buy items from market 1.
, and for all All footpaths take 1 minute to travel along.
for all and for all . Each market buys and sells every item, and the buying price of an item is equal to its selling price at any given market (it may be different between markets).

#### Sample Input

4 5 2
10 9 5 2
6 4 20 15
9 7 10 9
-1 -1 16 11
1 2 3
2 3 3
1 4 1
4 3 1
3 1 1

#### Sample Output

2

#### Explanation

In the sample case there are two cycles we will consider, "1 to 2 to 3 to 1" and "1 to 4 to 3 to 1".

Considering "1 to 2 to 3 to 1", we see that this cycle takes minutes to walk over . The most profitable sequence of trades on this cycle is to purchase item at market (cost of ), sell it at market (profit of ), immediately buy item from market (cost of ), carry item through market , and finally sell item at market (profit of ). Therefore in this cycle we can make profit in this profit cycle. rounded down gives an efficiency of for this cycle.

Considering "1 to 4 to 3 to 1", we see that this cycle takes minutes to walk over . The most profitable sequence of trades on this cycle is to purchase item at market (cost of ), sell it at market (profit of ), then walk through market and back to market . Therefore in this cycle we can make profit in this profit cycle. rounded down gives an efficiency of for this cycle.

Therefore the best efficiency of any profit cycle in Cobar is .