[思维]Minimum Spanning Tree
Posted lllxq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[思维]Minimum Spanning Tree相关的知识,希望对你有一定的参考价值。
题目描述
In the mathematical discipline of graph theory, the line graph of a simple undirected weighted graph G is another simple undirected weighted graph L(G) that represents the adjacency between every two edges in G.
Precisely speaking, for an undirected weighted graph G without loops or multiple edges, its line graph L(G) is a graph such that:
·Each vertex of L(G) represents an edge of G.
·Two vertices of L(G) are adjacent if and only if their corresponding edges share a common endpoint in G, and the weight of such edge between this two vertices is the sum of their corresponding edges‘ weight.
A minimum spanning tree(MST) or minimum weight spanning tree is a subset of the edges of a connected, edge-weighted undirected graph that connects all the vertices together, without any cycles and with the minimum possible total edge weight. That is, it is a spanning tree whose sum of edge weights is as small as possible.
Given a tree G, please write a program to find the minimum spanning tree of L(G).
Precisely speaking, for an undirected weighted graph G without loops or multiple edges, its line graph L(G) is a graph such that:
·Each vertex of L(G) represents an edge of G.
·Two vertices of L(G) are adjacent if and only if their corresponding edges share a common endpoint in G, and the weight of such edge between this two vertices is the sum of their corresponding edges‘ weight.
A minimum spanning tree(MST) or minimum weight spanning tree is a subset of the edges of a connected, edge-weighted undirected graph that connects all the vertices together, without any cycles and with the minimum possible total edge weight. That is, it is a spanning tree whose sum of edge weights is as small as possible.
Given a tree G, please write a program to find the minimum spanning tree of L(G).
输入
The first line of the input contains an integer T(1≤T≤1000), denoting the number of test cases.
In each test case, there is one integer n(2≤n≤100000) in the first line, denoting the number of vertices of G.
For the next n−1 lines, each line contains three integers u,v,w(1≤u,v≤n,u≠v,1≤w≤109), denoting a bidirectional edge between vertex u and v with weight w.
It is guaranteed that ∑n≤106.
In each test case, there is one integer n(2≤n≤100000) in the first line, denoting the number of vertices of G.
For the next n−1 lines, each line contains three integers u,v,w(1≤u,v≤n,u≠v,1≤w≤109), denoting a bidirectional edge between vertex u and v with weight w.
It is guaranteed that ∑n≤106.
输出
For each test case, print a single line containing an integer, denoting the sum of all the edges‘ weight of MST(L(G)).
样例输入 Copy
2
4
1 2 1
2 3 2
3 4 3
4
1 2 1
1 3 1
1 4 1
样例输出 Copy
8 4
题意:将原图中的边看作新图中的点,原图中的点看作新图中的边,且新图中的边权为它所连接的两个点的权值之和(即原图中的两边权之和),求新图的最小生成树。
思路:考虑对一个点来说,与其相连的所边要构成联通,其最小花费是什么?即找到权值最小的边,其他所有边都与它相连。
因为对于每一个点都要单独考虑,所以单独计算每一个点的该最小花费,求和即为答案。
以上是关于[思维]Minimum Spanning Tree的主要内容,如果未能解决你的问题,请参考以下文章
[LeetCode] Minimum Spanning Tree
算法练习:最小生成树 (Minimum Spanning Tree)
Minimum spanning tree for each edge