#P6270. [湖北省队互测 2014] 十万人的地铁
[湖北省队互测 2014] 十万人的地铁
Problem Description
Once upon a time, there were two people named Lu Renjia and Lu Renyi, and they went to take the subway.
Lu Renjia wanted to travel from Xunlimen to Guangbutun, while Lu Renyi wanted to travel from Guangbutun to Xunlimen.
As hardcore subway fans, Lu Renjia and Lu Renyi know the fare rules of the Wuhan Metro very well.
Wuhan Metro Line is a straight line with a total of stations, numbered in order as .
To take the subway, you need to tap a transit card called Wuhan Tong. You tap once when entering and once when exiting. When exiting, the system deducts a fare from the Wuhan Tong based on the distance between the start station and the destination station. That is, if denotes the cost of entering at station and exiting at station , then:
The clever Lu Renjia and Lu Renyi discovered that the start station of a trip is recorded on the Wuhan Tong when you tap in. Also, the fare does not depend on travel time: you can enter at station , play on the train all day, and exit at station , and you still pay .
So Lu Renjia and Lu Renyi had an idea and came up with a brilliant plan to ride for free.
First, Lu Renjia enters at Xunlimen and departs, then gets off and waits at the intermediate station Pangxiejia. At the same time, Lu Renyi enters at Guangbutun and departs, and also gets off and waits at Pangxiejia.
Then Lu Renjia and Lu Renyi meet at Pangxiejia and swap their Wuhan Tong cards.
Finally, Lu Renjia takes the subway to Guangbutun. At this time, the start station recorded on Lu Renjia's Wuhan Tong is Guangbutun, so the fare deducted is .
Lu Renyi takes the subway to Xunlimen. At this time, the start station recorded on Lu Renyi's Wuhan Tong is Xunlimen, so the fare deducted is also .
In this way, they complete one free ride.
Now Lu Renjia and Lu Renyi start thinking: suppose there are people taking the subway, numbered . Person travels from station to station with , taking the subway along a shortest path. That is:
- If , they pass through in order.
- If , they pass through in order.
Assuming they are smart enough to get off midway and swap tickets, what is the minimum total cost for their trips?
The ride procedure is defined as follows:
- At the beginning, everyone taps in. Person is at station .
- Each time, you may perform one of the following two operations:
- : Let person take the subway to station and get off. Detours are not allowed, meaning they must ride in the direction closer to . Also, traveling from the current station to the same station is not allowed, so cannot be the station number where person is currently located. You must ensure .
- : Let person and person swap their Wuhan Tong cards. Note that only two people who are currently at the same station can swap tickets. You must ensure .
- During each operation, anyone not mentioned in the operation stays where they are.
- After all operations, everyone taps out together. Note that you must ensure that person is at station at this time.
- Finally, after everyone exits, the sum of all fares deducted from the Wuhan Tong cards must be minimized. Find a minimum-cost ride plan.
Of course, Lu Renjia and Lu Renyi know how to do it. But they want to test you.
Input Format
The first line contains a positive integer , the number of test cases. Then follow test cases. For each test case:
The first line contains two positive integers separated by spaces, meaning there are people and subway stations.
The next lines each contain two positive integers separated by spaces, representing the start station and end station of person . ()
Output Format
For each test case, output an optimal plan. If multiple plans can achieve the minimum total deducted fare, output any one of them.
The first line contains two non-negative integers separated by spaces, representing the minimum total cost and the number of operations. The number of operations must not be too large, and must satisfy .
The next lines each contain three positive integers describing an operation. (, and the constraints on are as stated above)
2
3 7
1 7
1 6
5 1
2 7
1 7
7 1
7 5
0 1 5
1 3 1
0 1 7
0 2 6
0 3 1
0 3
0 1 7
1 2 1
0 2 1
Hint
For all test cases, .
| ID | ||
|---|---|---|
(This problem is purely an idea by VFleaKing... Tests show that entering and then exiting costs yuan, so please do not try to imitate the behavior described in the statement...)
Translated by ChatGPT 5