Cheapest Routes P40802


Statement
 

pdf   zip

We have collected abundant information about the local roads and accommodations in a region that we will traverse. Our plan is to go from city AA to city BB and we would like to spend the least possible money. For each road connecting two cities uu and vv we know the cost ω(u,v)=ω(v,u)\omega(u,v)=\omega(v,u) to travel along that road (tolls, fuel, meals during the journey, …). Every time we go from a city uu to one of its neighbors vv we must stop at vv and spend there one night; we know the cost ω(v)\omega'(v) of stopping at each city vv (the cost added by AA and BB to our route is 0, since they are our initial and final points). All costs, of vertices and of edges, are positive. Thus the cost of the route P=[A,v1,,vn,B]P = [A, v_1, \ldots, v_n, B] is cost(P)=ω(A,v1)+ω(v1,v2)++ω(vn,B)+ω(v1)++ω(vn).\text{cost}(P) = \omega(A,v_1) + \omega(v_1,v_2) + \ldots + \omega(v_n,B) + \omega'(v_1)+\ldots+\omega'(v_n).

Write a program in C++ which, given an undirected weighted graph with positive costs at the vertices and at the edges, and two vertices AA and BB, returns the cost of the cheapest route to go from AA to BB, or an indication that not such route exists.

Input

All data in the input are positive integers. The input starts with two integers 2n100002{\le}n{\le}10000 and mm, 0m20n0{\le}m\le{20n}. After that, a sequence of positive integers ω(0),,ω(n1)\omega'(0), \ldots, \omega'(n-1) of the weights ω(u)\omega'(u) of the nn vertices of the graph. Then the input contains a sequence of the mm edges in the graph as triplets of the form u,v,ω(u,v){\langle}u,v,\omega(u,v)\rangle. Vertices uu and vv are integers in {0,,n1}\{0,\ldots,n-1\} and the weights ω(u,v)\omega(u,v) are positive integers. No weight, of edges or vertices, is larger than 100000.

You can assume that there are no two different edges connecting the same pair of vertices nor any edge connecting a vertex to itself. Finally, there is a sequence of pairs Ai,Bi{\langle}A_i, B_i\rangle, with each AiA_i and BiB_i denoting vertices of the graph (0Ai,Bi<n0{\le}A_i,B_i<n).

Output

For each pair Ai,Bi{\langle}A_i, B_i\rangle in the input sequence the program writes the cost δ\delta of the cheapest route between AiA_i and BiB_i. with the format c(AiA_i,BiB_i) = δ\delta. If no route exists between AiA_i and BiB_i the program writes c(AiA_i,BiB_i) = +oo. The ouput for each case is ended with a newline (endl).

Public test cases
  • Input

    6 8
    3 6 10 15 5 2
    0 1 2  1 2 7  2 3 2 
    0 2 1  1 3 4  2 4 8
    3 4 2  3 0 5
    0 4
    1 4
    2 4
    3 1 
    4 1
    2 5
    2 2
    
    

    Output

    c(0,4) = 19
    c(1,4) = 21
    c(2,4) = 8
    c(3,1) = 4
    c(4,1) = 21
    c(2,5) = +oo
    c(2,2) = 0
    
  • Information
    Author
    Prof. EDA
    Language
    English
    Other languages
    Catalan Spanish
    Official solutions
    C++
    User solutions
    C++ Python