链接:https://www.nowcoder.com/acm/contest/123/C
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld
空间限制: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.
蒟蒻开始认为是最小生成树模板,但10的五次方的数据量无论Prim还是Kruscal都会超时。
正解是贪心思想,题目没有规定在哪些城市间修建道路,所以我们取权值最小的那座城市作为中心,用它将所有城市连接起来。
自己还差的太远。。。
AC代码:
#include<bits/stdc++.h>
#define INF 999999999;
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
long long s=0,tot=0,minn=INF;
for(int i=0;i<n;++i)
{
long long x;
cin>>x;
tot+=x;
if(x<minn)minn=x;
}
if(n==0)cout<<"0"<<endl;
else cout<< tot-minn+minn*(n-1)<<endl;
}
return 0;
}
加油吧!!!