NC15929. Cities
描述
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
C++11(clang++ 3.9) 解法, 执行用时: 66ms, 内存消耗: 504K, 提交时间: 2020-02-20 11:06:24
#include<iostream> using namespace std; int main() { int t; cin>>t; while(t--) { long long n,a,s=100001,sum=0; cin>>n; for(int i=0;i<n;i++) { cin>>a; if(a<s) s=a; sum+=a; } sum=sum+s*(n-2); cout<<sum<<endl; } }
Python3(3.5.2) 解法, 执行用时: 106ms, 内存消耗: 19388K, 提交时间: 2020-06-21 18:16:03
t=int(input()) while t: t-=1; n=int(input()) array=list(map(int,input().split(" "))) print(sum(array)+min(array)*(n-2))