Connecting Edges 2
InterviewTime limit2sMemory limit512 MB
Given a weighted edge list, pick the order of adding edges that makes the total weight added up to the moment s and t first become connected as small as possible.
- Level
Medium5 of 10
- Topics
- Graph, Sorting, Dynamic programming, Minimum spanning tree
- Solved
- No attempts yet
Problem
An undirected graph starts with vertices and edges. You are given an edge list of weighted edges. You add the edges of the list to the graph one at a time, in any order you like, and you stop the moment vertex and vertex become connected. Two vertices are connected when you can travel from one to the other along edges.
Choose the order that makes the total weight of the added edges as small as possible at the moment and become connected, and report that minimum.
Input
The first line contains the number of vertices and the number of edges in the list . (, )
Each of the next lines contains three integers , , , meaning the list holds an edge of weight between vertex and vertex . (, , )
The last line contains the two vertices and . (, )
The list may hold several edges between the same pair of vertices. Adding every edge of the list is guaranteed to make the graph connected.
Output
Print on the first line the minimum total weight of the edges added up to the moment and become connected.