Problem taken from the 5th Ho Chi Minh City High School Olympic 2018-2019.
There is a transport network including N nodes indexed from 1 to N, and M two-way roads connecting them together. Each node has a journey cost T. The cost for moving on each road connecting two nodes I and K is LIK=LKI. If one wants to move from a node to another, their cost will be the sum of all crossed roads' costs, plus the highest journey cost of the crossed nodes. This cost may be too big for them, so they have to calculate the minimum cost before deciding to move.
Task: Given transport demand between R pairs of transport nodes, calculate the minimum cost for moving between each pair.
Input: Given in text file CHIPHI.INP, including:
- The first line containing 3 integers N, M, R.
- N next lines, each contains an integer T, which corresponds to the journey cost of transport node 1 to N
- M next lines, each has 3 positive integers I, K and L (I ≠ K) expressing that moving on the road connecting two nodes I and K, will cost L.
- R next lines, each has 2 integers U and V (U ≠ V) expressing the demand of moving from transport node U to transport node V.
Output: Print to text file CHIPHI.OUT, including R lines, each contains a single integer corresponding to the minimum cost for moving between each pair of nodes.
Constraints:
- 1 ≤ N ≤ 250
- 1 ≤ M ≤ 10000
- 1 ≤ R ≤ 10000
- 1 ≤ T ≤ 100000
- 1 ≤ L ≤ 100000
CHIPHI.INP
5 7 2
2
5
3
3
4
1 2 3
1 3 2
2 5 3
5 3 1
5 4 1
2 4 3
3 4 4
1 4
2 3
CHIPHI.OUT
8
9
Explanation:
Consider a transport network of 5 nodes, 7 two-way roads and 2 pairs of transport nodes on demand.
Detailed costs as follows:
- Journey costs: T1=2, T2=5, T3=3, T4=3, T5=4
- Transport costs: L12=3, L13=2, L25=3, L53=1, L54=1, L24=3, L34=4.
Transport demand between 2 pairs of nodes: from node 1 to node 4, from node 2 to node 3.
- To move from node 1 to node 4 with the least cost, nodes need to be crossed in order: 1-3-5-4. The cost is (L13 + L53 + L54) + max{T1, T3, T5, T4} = (2+1+1)+4 = 8.
- To move from node 2 to node 3 with the least cost, nodes need to be crossed in order: 2-5-3. The cost is (L25 + L53) + max{T2, T5, T3} = (3+1)+5 = 9.