Two-Step Shortest Path 3
Time limit6sMemory limit1024 MB
Find the shortest path from X to Z in a weighted undirected graph that visits at least three of the P given intermediate vertices.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
Seojun was very happy to receive a world map from his father as a birthday present. He wants to develop a program that finds the shortest path on the world map and give it to his father as a token of thanks. The world map is an undirected graph whose vertices are cities and whose edges are roads between cities, and the length of a road is the weight of the edge. Help our Seojun by finding the shortest distance from the start vertex X to the destination vertex Z while passing through at least three of the P intermediate vertices.
Input
The first line gives the number of vertices N (10 ≤ N ≤ 100,000) and the number of edges M (10 ≤ M ≤ 300,000).
The next M lines give edge information u v w, describing a bidirectional road with integer weight w between city u and city v. (1 ≤ u, v ≤ N, u ≠ v, 1 ≤ w ≤ 1,000,000)
The next line gives X Z. (1 ≤ X, Z ≤ N, X ≠ Z)
The next line gives P. (3 ≤ P ≤ min(100, N - 3))
The next line gives P distinct intermediate vertices Y (1 ≤ Y ≤ N, X ≠ Y ≠ Z), separated by spaces.
Output
Print the shortest distance from the start vertex X to the destination vertex Z while passing through at least three of the P intermediate vertices. If the destination vertex Z cannot be reached, print -1.