Dijkstra's algorithm is capable of finding the shortest distance needed to reach any given node on a weighted graph from a starting position. Given a weighted graph, find the minimum distance needed to reach a series of nodes.
Input Specification
The first line of input is representing the number of nodes in the graph. They are numbered to .
The next line of input is representing the number of edges in the graph.
The next lines each contain 3 space separated integers, representing a connection between and of weight .
The next line is representing the number of queries about the graph that must be answered.
The next lines contains a node representing a query for the minimum distance to .
Output Specification
For each query and in order, print on a new line the minimum distance needed to reach node from node . If it is impossible to reach , print -1
.
Note: For Java use of Scanner may cause passable solutions to TLE. Faster input reading is recommended.
Sample Input
7
10
0 3 5
1 2 10
2 4 25
2 3 38
3 4 27
1 5 18
5 0 7
6 1 11
1 4 9
4 0 6
2
6
2
Sample Output
26
25
Comments
What are the constraints of ?
Between 1 and 1000 inclusive