#P6765. [APIO2020] 交换城市
[APIO2020] 交换城市
Description
Indonesia has cities and bidirectional roads. Cities are numbered from to , and roads are numbered from to . Each road connects two different cities. Road connects city and city , and a car will consume units of fuel to travel along this road. Using these roads, any two cities are reachable from each other.
Over the next days, each day a pair of cities wants to establish political relations. Specifically, on day , city wants to establish political relations with city . To do so, city will send a representative by car to city . Similarly, city will also send a representative by car to city .
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 togetMinimumFuelCapacity.- : An integer representing the number of cities.
- : An integer representing the number of roads.
- : An integer sequence of length representing the first endpoint city of each road.
- : An integer sequence of length representing the second endpoint city of each road.
- : An integer sequence of length representing the fuel consumption of each road.
-
getMinimumFuelCapacity(X, Y)- This function will be called by the grader exactly times.- : An integer representing the first city.
- : 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 and city respectively and travel to each other’s cities under the rules described above. If it is impossible to satisfy the rules, return .
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, , , , , , , , . 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 (it costs units of fuel to go from the third city to the second city). There is no better plan, so this function should return . -
getMinimumFuelCapacity(2, 4). Any car starting from the fourth city or arriving at the fourth city must consume units of fuel, so this function should return . -
getMinimumFuelCapacity(0, 1). This function should return .
In the second sample, , , , , , , , . 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 .
Constraints
- .
- .
- .
- Between any two cities, there is at most one road directly connecting them.
- Any two cities are reachable from each other via the roads.
- .
- .
- .
Subtask ( points)
- Each city is an endpoint of at most two roads.
Subtask ( points)
- .
- .
Subtask ( points)
- .
- .
- .
Subtask ( points)
- .
Subtask ( points)
- .
Subtask ( points)
- No additional constraints.
Translated by ChatGPT 5
京公网安备 11011102002149号