Minimum Spanning Tree

题目链接:http://codeforces.com/gym/102220/problem/E
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).

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≤10e6.

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

题意:
给定一棵 n 个点的树 G,每条边有权值。
G 的线图 L(G) 中连接 (u, v) 的边权为它们对应树上两条边的边权之和。
求 L(G) 的最小生成树的边权和。

思路:
考虑树上每个点 i 对答案的贡献。
如果 i 的度数小于 2,那么不会有贡献。
否则 i 连出去的这些边在线图中形成一个团。
一个团的最小生成树即为每个点向权值最小的点连边。

代码:

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
using namespace std;

#define pb push_back

typedef long long ll;

const int INF=0x3f3f3f3f;
const int MAX_N=100000+5;
const int MAX_M=50000000+5;

int n;
vector<int> G[MAX_N];

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=0;i<n;i++)G[i].clear();
        int v,u,w;
        for(int i=0;i<n-1;i++){
            scanf("%d%d%d",&v,&u,&w);
            v--;u--;
            G[v].pb(w);
            G[u].pb(w);
        }
        ll res=0;
        for(int i=0;i<n;i++){
            if(G[i].size()==1)continue;
            ll ans=0,minh=INF;
            for(int j=0;j<G[i].size();j++){
                ans+=G[i][j];
                minh=min(minh,(ll)G[i][j]);
            }
            res=res+ans+minh*(G[i].size()-2);
        }
        printf("%lld\n",res);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值