This page is still under construction.

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

Two-Step Shortest Path 2

Interview

Time limit1sMemory limit512 MB

Summary
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 XX, find the shortest distance to vertex ZZ when at least one of the PP intermediate vertices must be visited on the way.

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 of integer weight ww between city uu and city vv. (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. (1≤P≤N−31 \le P \le N - 3)

The next line gives the 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 vertex XX to vertex ZZ when at least one of the PP intermediate vertices must be visited on the way. If vertex ZZ cannot be reached, print -1.

Examples2

  1. Example 1

    Input
    13 19
    1 2 100
    1 3 100
    1 4 1
    2 5 1
    3 6 1
    3 4 1
    4 6 1
    4 7 1
    5 6 10
    5 8 10
    6 9 10
    7 10 1
    8 9 10
    8 11 1
    9 11 1
    9 12 1
    10 12 2
    11 13 1
    12 13 3
    1 13
    3
    8 9 10
    
    Expected output
    8
    
  2. Example 2

    Input
    13 19
    1 2 1
    1 3 2
    1 4 10
    2 5 1
    3 6 10
    3 4 10
    4 6 1
    4 7 1
    5 6 10
    5 8 1
    6 9 1
    7 10 1
    8 9 1
    8 11 100
    9 11 100
    9 12 100
    10 12 1
    11 13 100
    12 13 1
    1 13
    3
    8 9 10
    
    Expected output
    10