Railway Network
Time limit1sMemory limit128 MB
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 and (, ): the number of railway stations and the number of rail segments. Stations are numbered from to .
Each of the next lines contains three integers , , and (, , ): a rail segment between stations and with maintenance cost . No two segments connect the same pair of stations, and the network is connected.
The last line contains integers. The first is (), 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
