-->
当前位置:首页 > 题库

PROGRAMMING:Campus construction

Luz5年前 (2021-05-10)题库404
The campus is under construction. The road is the blood vessel of the campus. So the problem is, we now know the distance between each building. In order to save cost, we need to connect each building with the least cost. Each building is recorded as a point. We connect the shortest path so that each point is connected. If we can connect, we output the shortest path sum. Otherwise, if we can't connect, we output - 1( Each building is represented by 1,2... M)
###Input format:
First, enter the positive integer m, n (M: the number of buildings, N: the number of roads)
A1, B1, C1 (a, B are endpoints; C is the distance between a and b)
A2 B2 C2
A3 B3 C3
''''''''
An Bn Cn
###Output format:
If the shortest path can be found, the sum of paths will be output, otherwise - 1 will be output.
###Input example:
Here is a set of inputs. For example:
```in
9 14
1 2 4
2 3 8
3 4 7
4 5 9
5 6 10
6 7 2
7 8 1
8 9 7
2 8 11
3 9 2
7 9 6
3 6 4
4 6 14
1 8 8
```
###Output example:
The corresponding output is given here. For example:
```out
thirty-seven
```
###Input example:
Here is a set of inputs. For example:
```in
5 3
1 2 4
2 3 1
3 1 2
```
###Output example:
The corresponding output is given here. For example:
```out
-1
```







answer:If there is no answer, please comment