Description
Given a connected and undirected graph, a spanning tree of that graph is a subgraph that is a tree and connects all the
vertices together. A single graph can have many different spanning trees. We can also assign a weight to each edge,
and use this to assign a weight to a spanning tree by computing the sum of the weights of the edges in that spanning tree.
A minimum spanning tree (MST) is a spanning tree with weight less than or equal to the weight of every other spanning tree,
and the second minimum spanning tree is a spanning tree with weight strict more than the weight of minimum spanning tree.
Given a connected and undirected graph, you should calculate the weight of second minimum spanning tree.
Input
The first line contain an integer T(1 <= T <= 10), indicate the number of testcase.
For each testcase, the first line contain two numbers n, m(1<=n<=500, n - 1 <= m <= 1000), indicate the number of vertices
and edges. The next m lines, ecah contain 3 numbers x, y, l(1<=x,y<=n, x != y, 1 <= l <= 1000000), indicate an undirected
edge connect x and y with length l. There are no two edge connected the same vertices.
Output
For each testcase, print the weight of the second minimum spanning tree if exists, otherwise print -1.
题目来自移动信息工程学院短码之美月赛02
这道题要求次小生成树,假设最小生成树为T,其实T与次小生成树的区别就是一条T的边被一条T之外的边所替代了,所以对每一条T之外的边,比如这条边为E(a,b),在T中,a到b只有唯一一条路,那么加上E(a,b)能够组成回路,也就是说我们删掉T中的a到b的任意一条边节点还是联通的,所以我们只需要在T中找到a和b之间最长的那条边,把它替换掉。在不断地替换中找到次最小生成树。
|