logo AlgoBeat OnlineJudge
登录 注册

#209139. [GDCPC 2023] Classic Problem

内存限制:1024 MiB 时间限制:8000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

Given an undirected complete graph with vertices and triples where , it's guaranteed that , and for any two triples and with different indices we have .

For any two vertices and in the graph (), define the weight of the edge connecting them as follows:

  • If there exists a triple satisfying and , the weight of edge will be .
  • Otherwise, the weight of edge will be .

Calculate the total weight of edges in the minimum spanning tree of the graph.

输入格式

There are multiple test cases. The first line of the input contains an integer () indicating the number of test cases. For each test case:

The first line contains two integers and (, ) indicating the number of vertices in the graph and the number of triples.

For the following lines, the -th line contains three integers , and (, ) indicating the -th triple. It's guaranteed that for all we have .

It's guaranteed that the sum of of all test cases will not exceed .

输出格式

For each test case output one line containing one integer indicating the total weight of edges in the minimum spanning tree of the graph.

样例

样例输入 1

3
5 3
1 2 5
2 3 4
1 5 0
5 0
5 4
1 2 1000000000
1 3 1000000000
1 4 1000000000
1 5 1000000000

样例输出 1

4
4
1000000003

数据范围与提示

The first sample test case is illustrated as follows. The minimum spanning tree is marked by red segments.

The second sample test case is illustrated as follows. The minimum spanning tree is marked by red segments.

The third sample test case is illustrated as follows. The minimum spanning tree is marked by red segments.