This page is still under construction.

Parts of this page are still being built. What you see may change.

Two-Step Shortest Path 4

Time limit7sMemory limit1024 MB

Summary
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 XX to the destination vertex ZZ that passes through all PP intermediate vertices.

Input

The first line gives the number of vertices NN (10≤N≤100,00010 \le N \le 100{,}000) and the number of edges MM (10≤M≤300,00010 \le M \le 300{,}000).

The next MM lines give edge information u v w, meaning a bidirectional road between city uu and city vv with integer weight ww. (1≤u,v≤N1 \le u, v \le N, u≠vu \ne v, 1≤w≤1,000,0001 \le w \le 1{,}000{,}000)

The next line gives X Z. (1≤X,Z≤N1 \le X, Z \le N, X≠ZX \ne Z)

The next line gives PP. (3≤P≤min⁡(20,N−3)3 \le P \le \min(20, N - 3))

The next line gives PP distinct intermediate vertices YY (1≤Y≤N1 \le Y \le N, X≠Y≠ZX \ne Y \ne Z), separated by spaces.

Output

Print the shortest distance from the start vertex XX to the destination vertex ZZ that passes through all PP intermediate vertices. If the destination vertex ZZ cannot be reached, print -1.

Examples1

  1. Example 1

    Input
    10 16
    1 2 1
    1 3 100
    1 4 100
    1 5 100
    2 3 100
    2 6 1
    3 4 100
    3 6 100
    3 7 1
    4 5 100
    4 7 100
    4 8 1
    5 9 1
    6 10 1
    7 10 1
    9 10 1
    1 10
    3
    2 5 3
    
    Expected output
    11