Cities

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld

题目描述 

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

题解:要使所有城市连接起来,花费的费用最少,以为是两两连接,然而并不是,按照样例,只要找出一个最小的城市,然后其他所有城市和他连接就行,其他城市通过最小的城市中转连接起来。

#include
using namespace std;
long longt,n,a[100100],ans;
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;ans=0;
        for(int i=0; i>a[i],ans+=a[i];
        sort(a,a+n);
        if(n==1) ans=0;
        else  ans+=a[0]*(n-2);
        cout<

你可能感兴趣的:(牛客网,ACM日常水题练习集)