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 to city and we would like to spend the least possible money. For each road connecting two cities and we know the cost to travel along that road (tolls, fuel, meals during the journey, …). Every time we go from a city to one of its neighbors we must stop at and spend there one night; we know the cost of stopping at each city (the cost added by and 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 is
Write a program in C++ which, given an undirected weighted graph with positive costs at the vertices and at the edges, and two vertices and , returns the cost of the cheapest route to go from to , or an indication that not such route exists.
All data in the input are positive integers. The input starts with two integers and , . After that, a sequence of positive integers of the weights of the vertices of the graph. Then the input contains a sequence of the edges in the graph as triplets of the form . Vertices and are integers in and the weights 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 , with each and denoting vertices of the graph ().
For each pair
in the input sequence the program writes the cost
of the cheapest route between
and
.
with the format
c(,) = .
If no route exists between
and
the program writes
c(,) = +oo.
The ouput for each case is ended with a newline (endl).
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