#P13294. [GCJ 2013 #2] Ticket Swapping

[GCJ 2013 #2] Ticket Swapping

题目描述

The city has built its first subway line, with a grand total of NN stations, and introduced a new way of paying for travel. Instead of just paying for one ticket and making an arbitrary journey, the price you pay is now based on entry cards.

When entering the subway, each passenger collects an entry card, which specifies the station the passenger entered at. When leaving the subway, the passenger has to give up the entry card, and is charged depending on the distance (in stations traveled) between the entry station specified on the entry card, and the exit station on which the entry card is surrendered. The payment depends on the distance between these stations as follows:

  • if they are the same station, you don't pay;
  • if they are adjacent stations, you pay NN pounds;
  • if the distance is two stations, you pay 2N12N - 1: a charge NN for the first stop and N1N - 1 for the second;
  • the third station costs N2N-2 (so you pay 3N33N - 3 for a three-station-long trip), the fourth stop N3N-3, and the iith stop N+1iN + 1 - i;
  • thus, if you travel from one end of the subway to the other (a distance of N1N-1 stations), you pay 22 pounds for the last station traveled, and a grand total of (N2+N2)/2(N^2 + N - 2) / 2 in total.

After introducing this system the city noticed their gains are not as big as they expected. They figured out this might be due to people swapping their entry cards — so, for instance, if one person enters at station AA, travels two stations to BB and exits, while another person enters at BB, travels three stations to CC and exits, they would normally pay (in total) 2N1+3N3=5N42N - 1 + 3N - 3 = 5N - 4. But if the two people swapped their entry cards at station BB, then the first one would travel for free (as he would surrender an entry card specifying the station BB while exiting a station BB, and so register a distance of zero); while the second person will exit at station CC and surrender an entry card specifying station AA, which is 55 stations away, and pays 5N105N - 10, at a net loss of six pounds to the city!

The city now wants to learn how much they can possibly lose if this practice becomes widespread. We will consider only one direction (from station 11 to station NN, passing through all the stations in order) of the subway, and only one train on this line. We assume a passenger travelling from oo to ee obtains an entry card at oo, can swap her entry card any number of times with any other passengers anywhere between oo and ee, including swapping with people who leave at oo or those who enter at ee, and then exit the train at ee with some entry card (it is necessary to surrender some entry card to exit the subway). We also assume the passenger will not exit the train in the meantime (that is, will not surrender the currently held card and obtain a new one).

You are given a map of traffic (specifying how many passengers travel this train from which station to which), and you should calculate the city's financial loss, assuming passengers swap their cards to maximize this loss.

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case contains the number NN of stops (the stops are numbered 11 to NN), and the number MM of origin-endpoint pairs given. The next MM lines contain three numbers each: the origin stop oio_i, the end stop eie_i and pip_i: the number of passengers that make this journey.

输出格式

For each test case, output one line containing "Case #xx: yy", where xx is the case number (starting from 11) and yy is the total loss the city can observe due to ticket swapping, modulo 10000020131000002013.

3
6 2
1 3 1
3 6 1
6 2
1 3 2
4 6 1
10 2
1 7 2
6 9 1
Case #1: 6
Case #2: 0
Case #3: 10

提示

Sample Explanation

The first test case is the case described in the problem statement - two passengers meet at station 3 and swap tickets. In the second test case the two passengers don't meet at all, so they can't swap tickets (and so the city incurs no loss). In the third case, only one of the early passengers can swap tickets with the later passenger.

Limits

  • 1T201 \leq T \leq 20.
  • 1oi<eiN1 \leq o_i < e_i \leq N

Small dataset (8 Pts, Test set 1 - Visible)

  • 2N1002 \leq N \leq 100.
  • 1M1001 \leq M \leq 100.
  • 1pi1001 \leq p_i \leq 100.

Large dataset (11 Pts, Test set 2 - Hidden)

  • 2N1092 \leq N \leq 10^9.
  • 1M10001 \leq M \leq 1000.
  • 1pi1091 \leq p_i \leq 10^9.