#abc364f. F - Range Connect MST
F - Range Connect MST
Score : points
问题陈述
有一个图,包含 个顶点,编号为 。最初,图中没有边。
对于这个图,按照 的顺序执行以下操作:
- 对于满足 的每个整数 ,在顶点 和 之间添加一条成本为 的无向边。
确定所有操作完成后图是否连通。如果图是连通的,找出图的最小生成树的成本。
最小生成树是具有最小可能成本的生成树,生成树的成本是生成树中使用的边的成本之和。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
There is a graph with vertices, numbered . Initially, the graph has no edges.
For this graph, perform the following operation for in order:
- For each integer satisfying , add an undirected edge with cost between vertices and .
Determine if the graph is connected after all operations are completed. If it is connected, find the cost of a minimum spanning tree of the graph.
A minimum spanning tree is a spanning tree with the smallest possible cost, and the cost of a spanning tree is the sum of the costs of the edges used in the spanning tree.
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
If the graph is connected, print the cost of a minimum spanning tree. Otherwise, print .
Sample Input 1
4 3
1 2 2
1 3 4
2 4 5
Sample Output 1
22
The following edges form a minimum spanning tree:
- An edge with cost connecting vertices and
- An edge with cost connecting vertices and
- An edge with cost connecting vertices and
- An edge with cost connecting vertices and
- An edge with cost connecting vertices and
- An edge with cost connecting vertices and
Since , print .
Sample Input 2
6 2
1 2 10
4 6 10
Sample Output 2
-1
The graph is disconnected.
Sample Input 3
200000 4
1 200000 1000000000
1 200000 998244353
1 200000 999999999
1 200000 999999999
Sample Output 3
199651870599998