Under construction

Still being built. Things may change

Shortest Path Through Required Vertices

Interview

Time limit1sMemory limit256 MB

Summary
Given a weighted undirected graph, find the shortest path from vertex 1 to vertex N that must pass through two specified vertices.
Level

Medium5 of 10

Topics
Shortest path, Graph, Greedy
Solved
No attempts yet

Problem

An undirected weighted graph is given. Find the minimum distance from vertex 1 to vertex N among all paths that visit both of two specified distinct vertices v1 and v2.

Vertices and edges may be visited more than once. Only the total distance must be minimal among paths satisfying the requirement.

Input

The first line contains the number of vertices N and the number of edges E. (2 ≤ N ≤ 800, 0 ≤ E ≤ 200,000)

Each of the next E lines contains three integers a, b, and c, meaning that there is an undirected edge of distance c between vertices a and b. (1 ≤ c ≤ 1,000)

The last line contains the two distinct vertices v1 and v2 that must both be visited. (v1 ≠ v2, v1 ≠ N, v2 ≠ 1)

There is at most one edge between any pair of vertices.

Output

Print the length of the shortest path from vertex 1 to vertex N that visits both required vertices. If no such path exists, print -1.

Examples1

  1. Example 1

    Input
    4 6
    1 2 3
    2 3 3
    3 4 1
    1 3 5
    2 4 5
    1 4 4
    2 3
    
    Expected output
    7