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 n vertices and m edges of weight wi. 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−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.
在土耳其漫长而精彩的旅行中,你见过许多不同风格的镶嵌画,但从未见过这样一幅!
你眼前的这幅镶嵌画是一个包含 n 个顶点和 m 条边的图,其中第 i 条边的权重为 wi。你被它深深震撼,于是决定探寻其中隐藏的秘密信息。你考虑过多种可能性,目前的假设是:该秘密信息等于某组 n−1 条边的权重之和,该和在所有不构成树的 n−1 条边集合中取最小值。
首先,你想算出这一数值;至于它究竟意味着什么,你打算在回家的路上再细细琢磨。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains two integers n and m (2≤n≤2⋅105, n−1≤m≤2⋅105) — the number of vertices and edges in the graph, respectively.
The i-th of the following m lines contains three integers ui, vi, and wi (1≤ui=vi≤n, 1≤wi≤109) — the description of the i-th edge.
It is guaranteed that the graph does not contain self-loops or multiple edges.
It is guaranteed that the sum of n across all test cases does not exceed 2⋅105, and the sum of m across all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 m(2≤n≤2⋅105,n−1≤m≤2⋅105),分别表示图中的顶点数和边数。
接下来的 m 行中,第 i 行包含三个整数 ui、vi 和 wi(1≤ui=vi≤n,1≤wi≤109),表示第 i 条边的信息。
保证图中不含自环或重边。
保证所有测试用例的 n 之和不超过 2⋅105,且所有测试用例的 m 之和不超过 2⋅105。
输出格式
For each test case, output a single integer — the minimum sum of weights of a set of n−1 edges that does not form a tree, if such a set exists. Otherwise, print −1.
对于每个测试用例,输出一个整数——即不构成树的 n−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=10. It can be verified that this set of edges do not form a tree.
In the second test case, all possible subsets of 3 edges will form a tree. Hence, there is no solution.
在第一个测试用例中,你可以选择第二、第三和第六条边,其权重之和为 4+1+5=10。可以验证,该边集不能构成一棵树。
在第二个测试用例中,所有可能的包含 3 条边的子集均会构成一棵树。因此,不存在满足条件的解。
输入解题思路,AI测评打分。不知道怎么写?