Gym 102220E Minimum Spanning Tree

25 篇文章 1 订阅

Gym 102220E Minimum Spanning Tree

E. Minimum Spanning Tree
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output

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:
1.Each vertex of L(G) represents an edge of G.
2.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).

Input
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.

Output
For each test case, print a single line containing an integer, denoting the sum of all the edges’ weight of MST(L(G)).

Example
input

2
4
1 2 1
2 3 2
3 4 3
4
1 2 1
1 3 1
1 4 1

output

8
4

题目大意

先给你一个数 T ,表示有T组数据,然后再给你一个数 N ,表示每组数据有N个点,接下来给你N-1行数据,分别表示 边 U 与边 V 相连且权值 为 W ,现在让你把这些边看成点,如果之前两条边有公共点,那么这两条边换成点以后相连。这两个新点之间边的权值是之前这两条的权值之和。问你经过这个转换之后得到新图构建的最小生成树的权值最小是多少。

解题思路

这个题题目读起来太难了。理解之后发现我们只有计算一个点的度数就行了,并且计算最小的权值的时候只需要计算度数大于2的就行了,因为是要求权值和最小的,当一个点有多个度的时候我们就需要让这个点的权值最小的点乘以度数减一就行了,然后再加上其他的权值;
为什么呢?你想呀,如果变换之前一个点连接了多个点,那么变换之后这些边构成的图肯定是完全图,完全图变成一棵树肯定是用之前权值最小的边去链接其他的边,所以上面的操作就是对的了!(说的挺复杂的,建议手动画个图理解一下)

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

vector<ll>ve[100009];
int main()
{
	ios::sync_with_stdio(false);
	int t,n;
	int u,v,w;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=1;i<=n;i++) ve[i].clear();
		for(int i=1;i<n;i++)
		{
			cin>>u>>v>>w;
			ve[u].push_back(w);//把每个点后面的权值都放的这个点后面 
			ve[v].push_back(w);
		}
		ll sum=0;
		for(int i=1;i<=n;i++)
		{
			ll minn=1000900000;
			int len=ve[i].size();
			if(len>1)
		 	{
		 		for(int j=0;j<len;j++){
					sum+=ve[i][j];
					minn=min(minn,ve[i][j]);//找出最小值 
				}
				sum+=minn*(len-2);//因为之前加过一次了这次 sum+=minn*(len-2)	
			}	
		}
		cout<<sum<<"\n";
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值