Two-Step Shortest Path 4
Time limit7sMemory limit1024 MB
Given a weighted undirected graph, find the shortest walk from X to Z that visits every one of P intermediate vertices, with P at most 20.
- Level
Medium7 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 write a program that finds shortest paths on the world map to express his gratitude to his father. 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. Find the shortest distance from the start vertex to the destination vertex that passes through all intermediate vertices.
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 between city and city with integer weight . (, , )
The next line gives X Z. (, )
The next line gives . ()
The next line gives distinct intermediate vertices (, ), separated by spaces.
Output
Print the shortest distance from the start vertex to the destination vertex that passes through all intermediate vertices. If the destination vertex cannot be reached, print -1.