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.
Hard9GraphShortest pathBinary searchDynamic programmingNo attempts yetTime limit2sMemory limit512 MBAfter 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 N markets numbered from 1 to N, connected by M one-way footpaths, where walking along each path takes a given number of minutes.
The markets trade K different items numbered from 1 to K. 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 v with an empty backpack, follows footpaths while optionally buying and selling items, and returns to v 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 0.
Among all profit cycles with strictly positive duration, find the maximum efficiency, rounded down to an integer. If no such cycle exists, report 0.
Your program should read from standard input.
The first line contains three integers N, M and K, the numbers of markets, footpaths and items.
Then N lines follow. The ith of these lines contains 2K integers Bi,1,Si,1,Bi,2,Si,2,…,Bi,K,Si,K describing a market. For every 1≤j≤K, the pair Bi,j and Si,j are the prices at which you can buy and sell item j at market i. If an item cannot be bought or sold, −1 is used as a placeholder.
Then M lines follow. The pth of these lines contains three integers Vp, Wp and Tp, describing a one-way footpath from market Vp to market Wp that takes Tp minutes.
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, 1≤N≤100, 1≤M≤9900 and 1≤K≤1000. For every item that can be bought or sold, 0<Si,j≤Bi,j≤1000000000 for all 1≤i≤N and all 1≤j≤K. In addition, Vp=Wp and 1≤Tp≤10000000 for all 1≤p≤M, and there is no pair of paths 1≤p<q≤M with (Vp,Wp) equal to (Vq,Wq). That is, no directed pair of markets appears twice.
In the sample case there are two cycles to consider, one returning from 1 through 2 and 3 to 1, and one returning from 1 through 4 and 3 to 1.
The first cycle takes 3+3+1=7 minutes to walk. Its most profitable sequence of trades is to buy item 2 at market 1, sell it at market 2, then buy item 1 at market 2, carry it through market 3, and finally sell it at market 1. The profit is −5+15−6+9=13, and 13/7 rounded down gives efficiency 1.
The second cycle takes 1+1+1=3 minutes to walk. Its most profitable sequence of trades is to buy item 2 at market 1, sell it at market 4, and then pass through market 3 back to market 1. The profit is −5+11=6, and 6/3=2, so the efficiency is 2.
Therefore the best efficiency of any profit cycle is 2.