Travelling Merchant
Time limit2sMemory limit512 MB
Given a directed weighted graph and per-market buy/sell prices for K items, find the maximum profit-to-duration ratio of a closed walk trading at most one item at a time, floor it.
- Level
Hard9 of 10
- Topics
- Graph, Shortest path, Binary search, Dynamic programming
- Solved
- No attempts yet
Problem
After a long journey through the Australian outback, you arrive in the city of Cobar with nothing but a small backpack. Fascinated by the energy of its markets, you decide to settle there as a merchant. Cobar has markets numbered from to , connected by one-way footpaths, where walking along each path takes a given number of minutes.
The markets trade different items numbered from to . Each market sets a buying price and a selling price for each item. Not every market handles every item, and for a given item a market may support only buying or only selling. Assume that a market offering an item for sale has an unlimited supply, and that a market willing to buy an item keeps buying it without limit.
To earn money as quickly as possible, you want the most efficient profit cycle. A profit cycle is a walk that starts at some market with an empty backpack, follows footpaths while optionally buying and selling items, and returns to with an empty backpack. It may pass through the same market or footpath many times. A bought item must go into the backpack at once, and the backpack holds at most one item at any time. Assume you can always buy an available item regardless of how much money you hold, and that you cannot sell an item you do not hold.
The profit of a cycle is the total money received from sales minus the total money spent on purchases. Its duration is the sum of walking minutes along its footpaths. Its efficiency is profit divided by duration. A cycle with no trades has efficiency .
Among all profit cycles with strictly positive duration, find the maximum efficiency, rounded down to an integer. If no such cycle exists, report .
Input
Your program should read from standard input.
The first line contains three integers , and , the numbers of markets, footpaths and items.
Then lines follow. The th of these lines contains integers describing a market. For every , the pair and are the prices at which you can buy and sell item at market . If an item cannot be bought or sold, is used as a placeholder.
Then lines follow. The th of these lines contains three integers , and , describing a one-way footpath from market to market that takes minutes.
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.
Constraints
For all subtasks, , and . For every item that can be bought or sold, for all and all . In addition, and for all , and there is no pair of paths with equal to . That is, no directed pair of markets appears twice.
Hint
In the sample case there are two cycles to consider, one returning from through and to , and one returning from through and to .
The first cycle takes minutes to walk. Its most profitable sequence of trades is to buy item at market , sell it at market , then buy item at market , carry it through market , and finally sell it at market . The profit is , and rounded down gives efficiency .
The second cycle takes minutes to walk. Its most profitable sequence of trades is to buy item at market , sell it at market , and then pass through market back to market . The profit is , and , so the efficiency is .
Therefore the best efficiency of any profit cycle is .