#P13294. [GCJ 2013 #2] Ticket Swapping
[GCJ 2013 #2] Ticket Swapping
题目描述
The city has built its first subway line, with a grand total of 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 pounds;
- if the distance is two stations, you pay : a charge for the first stop and for the second;
- the third station costs (so you pay for a three-station-long trip), the fourth stop , and the th stop ;
- thus, if you travel from one end of the subway to the other (a distance of stations), you pay pounds for the last station traveled, and a grand total of 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 , travels two stations to and exits, while another person enters at , travels three stations to and exits, they would normally pay (in total) . But if the two people swapped their entry cards at station , then the first one would travel for free (as he would surrender an entry card specifying the station while exiting a station , and so register a distance of zero); while the second person will exit at station and surrender an entry card specifying station , which is stations away, and pays , 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 to station , passing through all the stations in order) of the subway, and only one train on this line. We assume a passenger travelling from to obtains an entry card at , can swap her entry card any number of times with any other passengers anywhere between and , including swapping with people who leave at or those who enter at , and then exit the train at 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, . test cases follow. Each test case contains the number of stops (the stops are numbered to ), and the number of origin-endpoint pairs given. The next lines contain three numbers each: the origin stop , the end stop and : the number of passengers that make this journey.
输出格式
For each test case, output one line containing "Case #: ", where is the case number (starting from ) and is the total loss the city can observe due to ticket swapping, modulo .
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
- .
Small dataset (8 Pts, Test set 1 - Visible)
- .
- .
- .
Large dataset (11 Pts, Test set 2 - Hidden)
- .
- .
- .