OCC '19 S6 - City Tolls

View as PDF

Submit solution

Points: 17 (partial)
Time limit: 2.0s
Memory limit: 256M

Author:
Problem types

bruce has recently been teleported to a strange new world. Here violence is common and the world has been split into tiny city-states. As such, to enter each city bruce will have to pay a certain entrance fee. Moreover, the only safe way to move through this world is through the roads which connect the cities. However, because cities are very careful, each city will be connected to at most 5 other cities. bruce being new to this world, and thinking ahead, has Q queries for you to calculate the danger of the least dangerous path from city ai to city bi. Because bruce would not like to be robbed, he wants to pay as little as possible at each city to not seem rich. As such, bruce defines the danger of a path as the largest entrance fee that he will have to pay on that path.

Note: For each query, bruce will start at city ai and as such will not need to pay the entrance fee of city ai.

Constraints

2N105

1M,Q105

1ai,biN

aibi

1vi109

Each city will be connected to at most 5 other cities.

No city will have a road to itself, and there will only be at max one road between any two cities.

Subtask 1 [15%]

2N103

1M,Q103

Subtask 2 [30%]

The road network will always be a tree.

Subtask 3 [55%]

No additional constraints.

Input Specification

The first line of input contains N, M, and Q representing the number of cities, roads, and queries respectively.

The next line contains N integers vi indicating the entrance cost of each city.

The next M lines contain two integers ai and bi denoting a road from city ai to bi.

The next Q lines contain two integers ai and bi requesting the danger of the least dangerous path from city ai to city bi.

Output Specification

For each query ai, bi, output the danger of the least dangerous path from city ai to city bi, and if there is no valid path, output -1.

Sample Input

Copy
6 7 5
4 3 2 5 1 3
1 2
2 5
3 1
4 2
5 1
5 4
4 3
1 4
5 3
2 3
1 5
1 6

Sample Output

Copy
5
4
4
1
-1

Explanation

One of the least dangerous paths from city 1 to city 4 is 134 paying entrance fees of 2 and 5 for cities 3 and 4 respectively, and having a danger of 5.

One of the least dangerous paths from city 5 to city 3 is 513 paying entrance fees of 4 and 2 for cities 1 and 3 respectively, and having a danger of 4.

One of the least dangerous paths from city 2 to city 3 is 213 paying entrance fees of 4 and 2 for cities 1 and 3 respectively, and having a danger of 4.

One of the least dangerous paths from city 1 to city 5 is 15 paying an entrance fee of 1 for city 5, and having a danger of 1.

There is no valid path from city 1 to city 6, thus the output is -1.


Comments

There are no comments at the moment.