链接:https://www.nowcoder.com/acm/contest/123/C
来源:牛客网
There are cities in Byteland, and the city has a value . The cost of building a bidirectional road between two cities is the sum of their values. Please calculate the minimum cost of connecting these cities, which means any two cities can reach each other.
输入描述:
The first line is an integer
representing the number of test cases.
For each test case, the first line is an integer , representing the number of cities, the
second line are positive integers ,
representing their values.
输出描述:
For each test case, output an integer, which is the
minimum cost of connecting these cities.
示例1
输入
2
4
1 2 3 4
1
1
输出
12
0
[题意]
有n个节点,每个有权值,两个相连的消费是连个节点的权值和,让所有点连接起来。
[分析]
就很无脑啊,所有节点都和最小的点连接嘛。为了写代码方便我就直接用排序了。
[代码]
#include<cstdio>
#include<algorithm>
using namespace std;
int a[100005];
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
long long ans = 0;
sort(a, a + n);
for (int i = 1; i < n; i++)
{
ans += a[i] + a[0];
}
printf("%lld\n", ans);
}
}