Cow Toll Paths

Time limit1sMemory limit128 MB

Summary
For each query, find the cheapest s-t trip where cost is the sum of edge tolls plus the single largest pasture toll on the route. N=250, K=10000.
Level

Hard8 of 10

Topics
Graph, Shortest path, Dynamic programming, Sorting
Solved
No attempts yet

Problem

Farmer John is always looking for ways to increase his revenue, so he has set up tolls that the cows must pay whenever they walk along the paths of his farm.

The farm has NN pastures (1≤N≤2501 \le N \le 250), numbered 11 through NN. They are connected by MM bidirectional paths (1≤M≤10,0001 \le M \le 10{,}000); path jj joins two different pastures AjA_j and BjB_j (1≤Aj,Bj≤N1 \le A_j, B_j \le N) and has an edge toll LjL_j (1≤Lj≤100,0001 \le L_j \le 100{,}000). Two pastures may be joined by more than one path, but no path connects a pasture to itself. Every pasture can be reached from every other pasture, so the graph is connected.

In addition, each pasture ii has its own toll CiC_i (1≤Ci≤100,0001 \le C_i \le 100{,}000). The cost of a trip from one pasture to a different pasture is the sum of the edge tolls of all paths traversed, plus a single additional toll equal to the maximum pasture toll among all pastures visited on the trip, including the starting and ending pastures.

The cows want to compare their options. Answer KK queries (1≤K≤10,0001 \le K \le 10{,}000). Query ii gives a starting pasture sis_i and an ending pasture tit_i (si≠tis_i \ne t_i); output the minimum possible cost of a trip from sis_i to tit_i.

Worked example. Consider five pastures with pasture tolls C1=2C_1=2, C2=5C_2=5, C3=3C_3=3, C4=3C_4=3, C5=4C_5=4, connected by paths with edge tolls (1,2)=3(1,2)=3, (1,3)=2(1,3)=2, (2,5)=3(2,5)=3, (3,5)=1(3,5)=1, (4,5)=1(4,5)=1, (2,4)=3(2,4)=3, (3,4)=4(3,4)=4, where (a,b)=w(a,b)=w means the path between pastures aa and bb has edge toll ww.

To travel from pasture 11 to pasture 44, take 1→3→5→41 \to 3 \to 5 \to 4: the edge tolls sum to 2+1+1=42+1+1=4 and the largest pasture toll on the route is 44 (pasture 55), so the total cost is 4+4=84+4=8.

To travel from pasture 22 to pasture 33, take 2→5→32 \to 5 \to 3: the edge tolls sum to 3+1=43+1=4 and the largest pasture toll on the route is 55 (pasture 22), so the total cost is 4+5=94+5=9.

Input

  • The first line contains three space-separated integers NN, MM, and KK.
  • Each of the next NN lines contains one integer; the ii-th of them is CiC_i, the toll of pasture ii.
  • Each of the next MM lines contains three space-separated integers AjA_j, BjB_j, and LjL_j, describing a bidirectional path between pastures AjA_j and BjB_j with edge toll LjL_j.
  • Each of the next KK lines contains two space-separated integers sis_i and tit_i, giving one query.

Output

  • Output KK lines. The ii-th line contains a single integer: the minimum possible cost of a trip from sis_i to tit_i.

Examples3

  1. Example 1

    Input
    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
    
    Expected output
    8
    9
    
  2. Example 2

    Input
    2 1 2
    10
    20
    1 2 5
    1 2
    2 1
    
    Expected output
    25
    25
    
  3. Example 3

    Input
    3 4 2
    1
    1
    100
    1 2 10
    1 2 4
    2 3 5
    1 3 50
    1 2
    1 3
    
    Expected output
    5
    109