CF2174D.Secret Message

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

During long excursions in Turkey, you have seen many different mosaics, but you have never encountered one like this!

The mosaic you see is a graph with nn vertices and mm edges of weight wiw_i. You are so impressed by it that you decided to search for a secret message contained within it. You have considered many different options, and your current hypothesis is that the secret is the sum of the weights of a set of n−1n - 1 edges such that the sum is minimal and the edges do not form a tree.

First, you want to find out this value, and what it means you plan to figure out on your way back home.

在土耳其漫长而精彩的旅行中,你见过许多不同风格的镶嵌画,但从未见过这样一幅!

你眼前的这幅镶嵌画是一个包含 nn 个顶点和 mm 条边的图,其中第 ii 条边的权重为 wiw_i。你被它深深震撼,于是决定探寻其中隐藏的秘密信息。你考虑过多种可能性,目前的假设是:该秘密信息等于某组 n−1n - 1 条边的权重之和,该和在所有不构成树的 n−1n - 1 条边集合中取最小值。

首先,你想算出这一数值;至于它究竟意味着什么,你打算在回家的路上再细细琢磨。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and mm (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5, n−1≤m≤2⋅105n - 1 \le m \le 2 \cdot 10^5) — the number of vertices and edges in the graph, respectively.

The ii-th of the following mm lines contains three integers uiu_i, viv_i, and wiw_i (1≤ui≠vi≤n1 \le u_i \ne v_i \le n, 1≤wi≤1091 \le w_i \le 10^9) — the description of the ii-th edge.

It is guaranteed that the graph does not contain self-loops or multiple edges.

It is guaranteed that the sum of nn across all test cases does not exceed 2⋅1052 \cdot 10^5, and the sum of mm across all test cases does not exceed 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5,n−1≤m≤2⋅105n - 1 \le m \le 2 \cdot 10^5),分别表示图中的顶点数和边数。

接下来的 mm 行中,第 ii 行包含三个整数 uiu_i、viv_i 和 wiw_i(1≤ui≠vi≤n1 \le u_i \ne v_i \le n,1≤wi≤1091 \le w_i \le 10^9),表示第 ii 条边的信息。

保证图中不含自环或重边。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5,且所有测试用例的 mm 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output a single integer — the minimum sum of weights of a set of n−1n - 1 edges that does not form a tree, if such a set exists. Otherwise, print −1-1.

对于每个测试用例,输出一个整数——即不构成树的 n−1n - 1 条边的权重之和的最小值(如果这样的边集存在);否则输出 −1-1。

输入输出样例

  • 输入#1

    4
    4 6
    1 2 7
    1 3 4
    1 4 1
    2 3 9
    2 4 6
    3 4 5
    4 4
    1 2 5
    2 3 5
    3 4 5
    1 4 8
    4 4
    1 4 1
    1 3 4
    2 4 2
    3 4 3
    4 4
    2 3 7
    1 2 5
    2 4 9
    4 3 12

    输出#1

    10
    -1
    8
    28

说明/提示

In the first test case, you can choose the second, third and sixth edge, with a weight sum of 4+1+5=104 + 1 + 5 = 10. It can be verified that this set of edges do not form a tree.

In the second test case, all possible subsets of 33 edges will form a tree. Hence, there is no solution.

在第一个测试用例中,你可以选择第二、第三和第六条边,其权重之和为 4+1+5=104 + 1 + 5 = 10。可以验证,该边集不能构成一棵树。

在第二个测试用例中,所有可能的包含 33 条边的子集均会构成一棵树。因此,不存在满足条件的解。

输入解题思路,AI测评打分。不知道怎么写?

首页