Two-Step Shortest Path 2
InterviewTime limit1sMemory limit512 MB
Given an undirected weighted graph, find the shortest distance from X to Z that passes through at least one of P given intermediate vertices.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, Heap, Dynamic programming
- Solved
- No attempts yet
Problem
Seojun was delighted to receive a world map from his father as a birthday present. He wants to write a program that finds shortest paths 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. Starting from vertex , find the shortest distance to vertex when at least one of the intermediate vertices must be visited on the way.
Input
The first line gives the number of vertices () and the number of edges ().
The next lines give edge information u v w, meaning a bidirectional road of integer weight between city and city . (, , )
The next line gives X Z. (, )
The next line gives . ()
The next line gives the distinct intermediate vertices (, ) separated by spaces.
Output
Print the shortest distance from vertex to vertex when at least one of the intermediate vertices must be visited on the way. If vertex cannot be reached, print -1.