#P85. [CSP-S 2022] Network test
[CSP-S 2022] Network test
Person in Charge
Attention
The memory limit for this problem is 1024 MB.
Problem Description
Little C is designing a routing system in a computer network.
The test network consists of hosts numbered from to in sequence. These hosts are connected by network cables, where the -th cable connects host and host . It is guaranteed that any two hosts can be connected directly or indirectly through a finite number of network cables. Restricted by the transmission power of information, a host can transmit information directly to a host if and only if the two hosts are connected by no more than network cables directly or indirectly.
In computer networks, data transmission often requires several forwarding steps. Suppose Little C needs to transmit data from host to host (). He will select a sequence of hosts for transmission , and forward the data according to the following rule: for all , host sends the information directly to host .
Each host requires a certain amount of time to process information, and the -th host takes units of time for processing. Data transmission in the network is extremely fast, so the transmission time is negligible. Thus, the time taken for the above transmission process is .
There are a total of data transmission requests. The -th request is to send data from host to host . Little C wants to know the minimum number of time units required to complete the transmission for each request.
Input Format
The first line of input contains three positive integers , representing the number of hosts in the network, the number of requests, and the transmission parameter respectively. The data guarantees , , .
The second line contains positive integers, where the -th integer denotes , with the constraint .
The next lines each contain two positive integers , representing a network cable connecting host and host . It is guaranteed that .
The next lines each contain two positive integers , representing a request to send data from host to host . It is guaranteed that and .
Output Format
Output lines, each with a positive integer representing the minimum time required for the -th transmission request.
Samples
7 3 3
1 2 3 4 5 6 7
1 2
1 3
2 4
2 5
3 6
3 7
4 7
5 6
1 2
12
12
3
Sample (1) Explanation
For the first request, since at least 4 network cables are required to connect host and host , data cannot be transmitted directly between the two hosts and requires at least one forwarding step. We choose to forward the data at host . It is easy to find that host is connected to both host and host with only two network cables, and the data processing time of host is only , which is the minimum among all hosts. Therefore, the minimum transmission time is .
For the third request, since host and host are connected by only one network cable, direct transmission of data is the optimal solution, with a minimum transmission time of .
Sample (2)
See transmit/transmit2.in and transmit/transmit2.ans in the contestant's directory.
This sample satisfies the constraints of Test Case 2.
Sample (3)
See transmit/transmit3.in and transmit/transmit3.ans in the contestant's directory.
This sample satisfies the constraints of Test Case 3.
Sample (4)
See transmit/transmit4.in and transmit/transmit4.ans in the contestant's directory.
This sample satisfies the constraints of Test Case 20.
Data Range
For all test data, it is guaranteed that , , , , and .
| Test Case No. | Special Property | |||
|---|---|---|---|---|
| Yes | ||||
| No | ||||
| Yes | ||||
| No | ||||
| Yes | ||||
| No | ||||
Special Property: It is guaranteed that , and is randomly selected from with equal probability.