C.Cities
原题链接
https://ac.nowcoder.com/acm/contest/123/C
思路
题意是最少花费多大的代价可以将所有城市连通,在两个城市之间建一条路的花费是两个城市的价值之和,那么直接贪心即可,找出价值最小的城市,用它来连接所有城市即可。
代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;
int a[N];
int main()
{
int t;
cin >> t;
while (t -- )
{
int n;
cin >> n;
if (n == 1)
{
cout << 0 << endl;
continue;
}
for (int i = 0; i < n; i ++ ) cin >> a[i];
sort(a, a + n);
long long sum = 0;
for (int i = 1; i < n; i ++ ) sum += a[0] + a[i];
cout << sum << endl;
}
return 0;
}