#P9600. [IOI 2023] 封锁时刻
[IOI 2023] 封锁时刻
Background
This is an interactive problem. You only need to implement the function required in the code.
Your code does not need to include any additional header files, and you do not need to implement the main function.
This problem only supports submissions in C++.
Problem Description
There are cities in Hungary, numbered from to .
These cities are connected by bidirectional roads, numbered from to . For each (), road connects city and city , and has length , meaning the travel time between these two cities is time units. Each road connects two different cities, and between any two cities there is at most one road.
A path between two different cities and is a sequence of distinct cities satisfying:
- ,
- ,
- for each (), there is a road connecting and .
Using these roads, it is possible to travel from any city to any other city. In other words, there is a path between any two different cities.
It can be proven that the path between any two different cities is unique.
The length of a path is the sum of the lengths of the roads connecting consecutive cities along the path.
In Hungary, many people attend celebrations held in two major cities on National Day. When the celebrations end, they go home. To prevent crowds from disturbing local residents, the government decided to block cities at certain moments. Each city is assigned a non-negative blockade moment. The government decided that the sum of blockade moments of all cities must not exceed . Specifically, for each (), the blockade moment assigned to city is a non-negative integer . The sum of all does not exceed .
Consider a city and an assignment of blockade moments. We say that a city is reachable from city if and only if at least one of the following two cases holds.
Case 1: .
Case 2: The path between the two cities ( and ) satisfies:
- the length of the path is at most , and
- the length of the path is at most , and
- the length of the path is at most .
This year, the two main celebration locations are in cities and .
For each assignment of blockade moments, we define a convenience score as the sum of the following two numbers:
- the number of cities reachable from city ,
- the number of cities reachable from city .
Note that if a city is reachable from both city and city , then it is counted twice when computing the convenience score.
Your task is to compute the maximum convenience score that can be achieved by some assignment of blockade moments.
Input Format
Let be the number of scenarios, i.e. the number of calls to max_score. The grader reads the input in the following format:
- Line :
The following are the descriptions of the scenarios.
The grader reads the description of each scenario in the following format:
- Line :
- Line ():
Output Format
For each scenario, the grader prints a separate line in the following format:
- Line : the return value of
max_score
2
7 0 2 10
0 1 2
0 3 3
1 2 4
2 4 2
2 5 5
5 6 3
4 0 3 20
0 1 18
1 2 1
2 3 19
6
3
Hint
Implementation Details
You need to implement the following function.
int max_score(int N, int X, int Y, int64 K, int[] U, int[] V, int[] W)
- : the number of cities.
- , : the two major celebration cities.
- : the upper bound on the total sum of blockade moments.
- , : arrays of length describing the road connections.
- : an array of length describing the road lengths.
- The function should return the maximum convenience score achievable by some assignment of blockade moments.
- This function may be called multiple times for each test case.
Examples
Consider the following call:
max_score(7, 0, 2, 10,
[0, 0, 1, 2, 2, 5], [1, 3, 2, 4, 5, 6], [2, 3, 4, 2, 5, 3])
This corresponds to the following road network:

Suppose the blockade moments are assigned as follows:
| City | |||||||
|---|---|---|---|---|---|---|---|
| Blockade moment | |||||||
Note that the sum of all blockade moments is , which does not exceed . Cities , , and are reachable from city (), and cities , , and are reachable from city (). Therefore, the convenience score is . There is no assignment of blockade moments that makes the convenience score greater than , so the function should return .
Consider another call:
max_score(4, 0, 3, 20, [0, 1, 2], [1, 2, 3], [18, 1, 19])
This corresponds to the following road network:

Suppose the blockade moments are assigned as follows:
| City | ||||
|---|---|---|---|---|
| Blockade moment |
City is reachable from city (), and cities and are reachable from city (). Therefore, the convenience score is . There is no assignment of blockade moments that makes the convenience score greater than , so the function should return .
Constraints
- (for each with )
- (for each with )
- Using these roads, you can travel from any city to any other city.
- , where is the sum of over all calls to
max_score.
Subtasks
We say a road network is linear if road connects city and city (for each with ).
- (8 points) The length of the path from city to city is greater than .
- (9 points) , and the road network is linear.
- (12 points) , and the road network is linear.
- (14 points) , and the road network is linear.
- (9 points) .
- (11 points) .
- (10 points) .
- (10 points) .
- (17 points) No additional constraints.
Translated by ChatGPT 5