#P6765. [APIO2020] 交换城市

[APIO2020] 交换城市

Description

Indonesia has NN cities and MM bidirectional roads. Cities are numbered from 00 to N1N - 1, and roads are numbered from 00 to M1M - 1. Each road connects two different cities. Road ii connects city U[i]U[i] and city V[i]V[i], and a car will consume W[i]W[i] units of fuel to travel along this road. Using these roads, any two cities are reachable from each other.

Over the next QQ days, each day a pair of cities wants to establish political relations. Specifically, on day jj, city X[j]X[j] wants to establish political relations with city Y[j]Y[j]. To do so, city X[j]X[j] will send a representative by car to city Y[j]Y[j]. Similarly, city Y[j]Y[j] will also send a representative by car to city X[j]X[j].

To avoid congestion, the two cars should not meet at any point in time. More specifically, the two cars cannot be in the same city at the same time. Likewise, the two cars should not travel along the same road in opposite directions at the same time. Also, when a car travels along a road, it must traverse the entire road and arrive at the city on the other end (in other words, a car is not allowed to turn around in the middle of a road). However, a car may visit a city multiple times or traverse a road multiple times. In addition, a car may wait at any city for any amount of time.

Because cars with high fuel capacity are expensive, each of the two cities wants to choose a route so that the maximum fuel capacity needed by the two cars is minimized. Each city has a gas station with unlimited supply, so the fuel capacity needed by a car is actually the maximum fuel consumption among the roads it travels.

You must implement the functions init and getMinimumFuelCapacity.

  • init(N, M, U, V, W) - This function will be called by the grader exactly once before any calls to getMinimumFuelCapacity.

    • NN: An integer representing the number of cities.
    • MM: An integer representing the number of roads.
    • UU: An integer sequence of length MM representing the first endpoint city of each road.
    • VV: An integer sequence of length MM representing the second endpoint city of each road.
    • WW: An integer sequence of length MM representing the fuel consumption of each road.
  • getMinimumFuelCapacity(X, Y) - This function will be called by the grader exactly QQ times.

    • XX: An integer representing the first city.
    • YY: An integer representing the second city.
    • This function must return an integer, the minimum possible value of the maximum fuel capacity among the two cars that start from city XX and city YY respectively and travel to each other’s cities under the rules described above. If it is impossible to satisfy the rules, return 1−1.

Input Format

The sample grader will read input in the following format:

N M
U[0] V[0] W[0]
U[1] V[1] W[1]
.
.
.
U[M-1] V[M-1] W[M-1]
Q
X[0] Y[0]
X[1] Y[1]
.
.
.
X[Q-1] Y[Q-1]

Output Format

For each call to getMinimumFuelCapacity, the sample grader will output the return value of that function.

5 6
0 1 4
0 2 4
1 2 1
1 3 2
1 4 10
2 3 3
3
1 2
2 4
0 1

3
10
4

3 2
0 1 5
0 2 5
1
1 2

-1

Hint

In the first sample, N=5N = 5, M=6M = 6, U=[0,0,1,1,1,2]U = [0, 0, 1, 1, 1, 2], V=[1,2,2,3,4,3]V = [1, 2, 2, 3, 4, 3], W=[4,4,1,2,10,3]W = [4, 4, 1, 2, 10, 3], Q=3Q = 3, X=[1,2,0]X = [1, 2, 0], Y=[2,4,1]Y = [2, 4, 1]. As shown in the figure:

The grader will first call init(5, 6, [0, 0, 1, 1, 1, 2], [1, 2, 2, 3, 4, 3],[4, 4, 1, 2, 10, 3]). Then the grader will make the following function calls:

  • getMinimumFuelCapacity(1, 2). First, the car starting from the first city can travel to the third city. Next, the car starting from the second city can travel to the first city, and the car at the third city can travel to the second city. Therefore, the maximum fuel capacity is 33 (it costs 33 units of fuel to go from the third city to the second city). There is no better plan, so this function should return 33.

  • getMinimumFuelCapacity(2, 4). Any car starting from the fourth city or arriving at the fourth city must consume 1010 units of fuel, so this function should return 1010.

  • getMinimumFuelCapacity(0, 1). This function should return 44.

In the second sample, N=3N = 3, M=2M = 2, U=[0,0]U = [0, 0], V=[1,2]V = [1, 2], W=[5,5]W = [5, 5], Q=1Q = 1, X=[1]X = [1], Y=[2]Y = [2]. As shown in the figure:

The grader will first call init(3, 2, [0, 0], [1, 2], [5, 5]), and then the grader will make the following function call:

  • getMinimumFuelCapacity(1, 2). The two cars cannot satisfy the requirement of not meeting at the same time, so this function should return 1-1.

Constraints

  • 2N1000002 \leq N \leq 100 000.
  • N1M200000N - 1 \leq M \leq 200 000.
  • 0U[i]<V[i]<N0 \leq U[i] < V [i] < N.
  • Between any two cities, there is at most one road directly connecting them.
  • Any two cities are reachable from each other via the roads.
  • 1W[i]1091 \leq W[i] \leq 10^9.
  • 1Q2000001 \leq Q \leq 200 000.
  • 0X[j]<Y[j]<N0 \leq X[j] < Y [j] < N.

Subtask 11 (66 points)

  • Each city is an endpoint of at most two roads.

Subtask 22 (77 points)

  • M=N1M = N - 1.
  • U[i]=0U[i] = 0.

Subtask 33 (1717 points)

  • Q5Q \leq 5.
  • N1000N \leq 1 000.
  • M2000M \leq 2 000.

Subtask 44 (2020 points)

  • Q5Q \leq 5.

Subtask 55 (2323 points)

  • M=N1M = N - 1.

Subtask 66 (2727 points)

  • No additional constraints.

Translated by ChatGPT 5