This page is still under construction.

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

Railway Network

Time limit1sMemory limit128 MB

Summary
Given a connected edge-weighted graph and at most 8 terminals, find the minimum cost edge set that keeps all terminals mutually connected.
Level

Medium7 of 10

Topics
Graph, Shortest path, Minimum spanning tree, Dynamic programming
Solved
No attempts yet

Problem

Byteland Railways is restructuring and shrinking its rail network. It has already been decided which stations will be kept and which will be removed, and the goal now is to make the network as cheap to maintain as possible. What remains is to choose which rail segments to keep and which to remove.

The network is made of rail segments, each connecting two railway stations. Originally you can travel between any two stations, possibly passing through intermediate ones. Every rail segment is bidirectional, at most one segment connects any given pair of stations, and each segment has a maintenance cost that is a positive integer.

You must keep a set of rail segments so that:

  • every pair of stations that will be kept stays mutually reachable, and
  • the total maintenance cost of the kept segments is as small as possible.

A kept railway line may pass through stations that are being removed, so a removed station can still act as an intermediate point on a route. All other segments are removed.

Given the network and the set of stations that will be kept, compute the smallest possible total maintenance cost of the kept segments.

Input

The first line contains two integers nn and mm (2≤n≤1002 \le n \le 100, 1≤m≤n(n−1)21 \le m \le \frac{n(n-1)}{2}): the number of railway stations and the number of rail segments. Stations are numbered from 11 to nn.

Each of the next mm lines contains three integers aa, bb, and uu (1≤a,b≤n1 \le a, b \le n, a≠ba \ne b, 1≤u≤1000001 \le u \le 100000): a rail segment between stations aa and bb with maintenance cost uu. No two segments connect the same pair of stations, and the network is connected.

The last line contains p+1p + 1 integers. The first is pp (1≤p≤min⁡(n,8)1 \le p \le \min(n, 8)), the number of stations that will be kept, followed by their numbers in increasing order.

Output

Output a single integer: the minimum total maintenance cost of a set of rail segments such that every kept station can reach every other kept station. Kept segments may pass through stations that are being removed.

Hint

Examples3

  1. Example 1

    Input
    8 11
    1 2 6
    3 1 5
    2 3 8
    3 4 9
    3 5 10
    5 4 3
    5 6 9
    6 4 8
    6 8 8
    6 7 7
    8 7 10
    4 2 5 7 8
    
    Expected output
    42
    
  2. Example 2

    Input
    2 1
    1 2 5
    1 1
    
    Expected output
    0
    
  3. Example 3

    Input
    4 4
    1 2 2
    2 3 3
    3 4 4
    1 4 100
    2 1 4
    
    Expected output
    9