#P7359. 「JZOI-1」旅行
「JZOI-1」旅行
Description
This trip takes place in a country with cities and bidirectional roads, and it is guaranteed that any two cities are reachable from each other.
To beautify the environment, all roads are built along rivers. This means Xiao Xi can make a boat by himself and then row along a road, so each time he traverses an edge, he may either walk on land or go by boat on the river.
Of course, because of downstream and upstream effects, there is a parameter . In other words, if the time to walk across this edge on land is , then the time to row downstream is (guaranteed to be greater than ), and the time to row upstream is . However, building a boat takes time, and once a person gets on land, they must abandon the boat.
Now Xiao Xi wants you to help compute the shortest time from to .
Note: A boat can be used continuously for multiple water segments (as long as you do not get off the boat).
Input Format
The first line contains three integers .
The next lines each contain five integers , where means the water flows from to , and means the water flows from to .
The next lines each contain two integers , representing the start and end of each planned trip.
Output Format
Output lines, each containing one number, the answer for each query.
3 2 2
1 2 2 1 0
1 3 3 2 1
2 3
1 2
4
2
4 1 1
1 2 100 99 1
2 3 100 99 0
3 4 100 99 1
1 4
104
Hint
Explanation of Sample 1
The graph looks like this:

For the first query, from to , we can build a boat at node , costing time, then go downstream from node to , costing , and then go downstream to , costing . So the total cost is .
Constraints
For of the testdata, .
For another of the testdata, the shape of the tree is random.
For another of the testdata, all are the same, or all are the same.
The last test point provides a chain.
For of the testdata, , and .
Translated by ChatGPT 5
京公网安备 11011102002149号