Passengers riding one direction on a line pay a decreasing per-stop fare and may swap entry cards where trips overlap, so compute the maximum total fare loss.
Hard8GreedySortingPrefix sumMathNo attempts yetTime limit5sMemory limit512 MBThe city opened its first subway line. It has N stations, numbered 1 to N, and fares are charged through entry cards instead of tickets.
A passenger entering the subway receives one entry card, which records the station where the passenger entered. On the way out the passenger has to give up an entry card, and pays according to the distance between the station printed on the card and the station where the card is surrendered.
After introducing this system the city noticed that its revenue was smaller than expected. The cause was passengers swapping entry cards. Suppose one person enters at station A, travels two stations and leaves at station B, while another person enters at station B, travels three stations and leaves at station C. Normally the two pay 2N−1+3N−3=5N−4 in total. If they swap their cards at station B, the first person surrenders a card printed with B at station B, registers a distance of zero and travels for free. The second person surrenders a card printed with A at station C, a distance of 5 stations, and pays 5N−10. The city loses six pounds.
The city wants to know how much it can lose if this practice becomes widespread. Consider only one direction of the line (from station 1 to station N, passing through every station in order) and only one train. A passenger travelling from o to e obtains a card at o, can swap that card any number of times with any other passenger anywhere between o and e, including passengers who leave at o and passengers who enter at e, and then leaves at e after surrendering some card. Surrendering a card is required in order to leave. A passenger never leaves the train in the middle, so nobody surrenders the card being held and obtains a new one.
You are given a map of traffic that says how many passengers travel from which station to which station. Compute the loss of the city when passengers swap their cards so that this loss is as large as possible. The loss is the total fare when nobody swaps a card, minus the smallest total fare that swapping can produce.
The first line of the input contains the number of test cases T. The first line of each test case contains the number of stations N and the number M of origin and endpoint pairs. Stations are numbered 1 to N. Each of the next M lines contains three integers oi, ei and pi, meaning that pi passengers enter at station oi and leave at station ei.
Limits:
For each test case, print one line in the format Case #x: y, where x is the test case number starting from 1, and y is the loss of the city caused by card swapping, modulo 1000002013.
The first example case is the situation described in the statement: the two passengers meet at station 3 and swap their cards. In the second example case the two passengers never share a stretch of the line, so they cannot swap and the city loses nothing. In the third example case only one of the two earlier passengers can swap cards with the later passenger.