Codeforces Round 1069 (Div. 1)
D
Secret Message
Codeforces
3400
原题
← C2E1 →
翻译中…

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 $w_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 - 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.

翻译中…

Input

翻译中…

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

翻译中…

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

翻译中…

The $i$-th of the following $m$ lines contains three integers $u_i$, $v_i$, and $w_i$ ($1 \le u_i \ne v_i \le n$, $1 \le w_i \le 10^9$) — 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 \cdot 10^5$, and the sum of $m$ across all test cases does not exceed $2 \cdot 10^5$.

翻译中…

Output

翻译中…

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

翻译中…

Note

翻译中…

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.

翻译中…

样例

Input #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
Output #1
10
-1
8
28
InputCopy OutputCopy
题目信息
题号
2174D
时间限制
2 s
内存限制
256 MB
标签
数据结构
动态规划
贪心
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。