#P3778. [APIO2017] 商旅

[APIO2017] 商旅

Description

After a long trek across the vast Australian outback, you arrive alone in Cobar with a backpack. Captivated by the city’s prosperous and beautiful markets, you decide to settle down and become a merchant. Cobar has NN markets, numbered from 11 to NN, connected by MM directed roads. Traversing each road takes some amount of time.

There are KK different types of goods in Cobar, numbered from 11 to KK. Each market has its own prices for each good, and the buy and sell prices may differ. Not every market buys and sells every good: a market may support both buying and selling only for a subset of goods; for a particular good, a market may only buy but not sell, or only sell but not buy. If a market buys a certain good, the quantity it buys is unlimited; similarly, if a market sells a certain good, the quantity it sells is unlimited.

To gain profit more quickly, you decide to find the cycle with the highest profit efficiency. A cycle means starting from some market with an empty backpack, traveling along roads through several markets, and finally returning to the starting point. It is allowed to pass through the same market or the same road multiple times within the cycle. When passing a market, you may buy or sell goods. Once you buy a good, you must carry it in your backpack. Because your backpack is very small, you can hold at most one good at any time. When buying a good, you do not need to consider whether you have enough money; when selling, note that you can only sell the good you currently hold.

The profit gained from a cycle is the total money obtained from selling minus the total money spent on buying during the cycle. The time spent on a cycle is the sum of the traversal times of all roads along the cycle. The profit efficiency of a cycle is the profit divided by the time spent. Note that the profit efficiency of a cycle with no transactions is 00.

You need to find, among all cycles with strictly positive total time, the maximum profit efficiency. Output the value floored to an integer. If no cycle can yield a positive profit, output 00.

Input Format

The first line contains 33 positive integers NN, MM, and KK, denoting the number of markets, the number of roads, and the number of goods, respectively.

The next NN lines describe the markets. In line ii, there are 2K2K integers $B_{i,1}, S_{i,1}, B_{i,2}, S_{i,2}, \cdots, B_{i,K}, S_{i,K}$. For any 1jK1 \le j \le K, the integers Bi,jB_{i,j} and Si,jS_{i,j} denote the buying and selling price at market ii for good jj, respectively. If a transaction price is 1-1, then that transaction is not available for that good at that market.

Then follow MM lines. In line pp, there are 33 integers Vp,Wp,TpV_p, W_p, T_p, indicating there is a directed road from market VpV_p to market WpW_p with travel time TpT_p minutes.

Output Format

Output a single integer: the maximum profit efficiency over all cycles, floored to an integer. If no cycle can be profitable, output 00.

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
2

Hint

Sample explanation.

In the sample, consider the two cycles 1−2−3−1 and 1−4−3−1.

Consider the cycle 1−2−3−1: the total time is (3+3+1)=7(3+3+1)=7 minutes. The best sequence of transactions is: at market 11, buy good 22 (spending 55); at market 22, sell good 22 (gaining 1515), then immediately buy good 11 (spending 66); pass through market 33 carrying good 11, and upon returning to market 11, sell it (gaining 99). The total profit is 5+156+9=13-5+15-6+9=13. The profit efficiency is 137\frac{13}{7}, whose floor is 11.

Consider the cycle 1−4−3−1: the total time is (1+1+1)=3(1+1+1)=3 minutes. The best sequence of transactions is: at market 11, buy good 22 (spending 55); at market 44, sell good 22 (gaining 1111); then pass through market 33 and return to market 11. The total profit is 5+11=6-5+11=6. The profit efficiency is 63\frac 6 3, whose floor is 22.

Therefore, the maximum profit efficiency over all cycles is 22.

Subtasks.

Across all testdata, the following hold: 1N1001 \le N \le 100, 1M99001 \le M \le 9900, 1K10001 \le K \le 1000. If in market ii for good jj both buying and selling are available, then 0Si,jBi,j1090 \le S_{i,j} \le B_{i,j} \le 10^9. For road pp (1pM1 \le p \le M), it is guaranteed that VpWpV_p \ne W_p and 1Tp1071 \le T_p \le 10^7. There do not exist indices p,qp, q with 1pqM1 \le p \le q \le M such that (Vp,Wp)=(Vq,Wq)(V_p, W_p) = (V_q, W_q).

Subtask Score Additional conditions Explanation
11 1212 Bi,j=1 (2iN,1jK)B_{i,j}=-1\ (2\le i\le N, 1\le j\le K) You can only buy goods at market 11.
22 2121 N,K50, Tp=1 (1pM)N, K \le 50, \ T_p=1\ (1\le p\le M) All roads take 11 minute to traverse.
33 3333 Bi,j=Si,j1 (1iN,1jK)B_{i,j}=S_{i,j}\neq -1\ (1\le i\le N, 1\le j\le K) At each market, the buy and sell prices are the same (prices may differ across markets).
44 3434 None None.

Translated by ChatGPT 5