Minimum Valid Path

3.2

4 votes
Algorithms, Dijkstra's algorithm, Graphs, Medium
Problem

You are given a weighted directed graph some of whose vertices are marked as special vertices. A valid path in it is a path which satisfies the following conditions:

1. For any two adjacent edges x and y on the path, 0.5*weight() <= weight() <= 2*weight()

2. The path contains exactly one special vertex.

Given two vertices and , your task is to calculate the minimum cost of a valid path between them.


Input Format

First line:  and , the number of vertices and the number of edges in the graph.
(, )

Next lines contain three integers , , and representing an edge from vertex to vertex , with a weight of e. (, )

Next line contains an integer - the number of special vertices. ()

Next line contains distinct integers, the indices of the special vertices. ()

The last line contains the integers and , the source and destination vertices.
()

Note: Graph can have multiple edges between two vertices.

Output Format

If there is no valid path from to output -1, else output the minimum cost of a valid path from to .

Sample Input
4 4
1 2 1
2 3 1
3 4 1
1 3 1
1
4
1 4
Sample Output
2
Time Limit: 4
Memory Limit: 256
Source Limit:
Explanation

In the given sample, it is clear that path 1-3-4 is a valid path with cost 2. Though 1-2-3-4 is also a valid path, but its cost is 3. So, 2 is the minimum cost of a valid path.

 

Editor Image

?